0

I need to resize an image before saving to the database, so i am overriding the save method:

from PIL import Image

class Categorias(models.Model):
image_height = models.PositiveIntegerField(editable=False,null=True)
image_width = models.PositiveIntegerField(editable=False,null=True)
the_image = models.ImageField(upload_to="uploads/image/category",width_field="image_width",height_field="image_height")

def save(self):
    if not self.id and not self.the_image:
        return;

    image = Image.open(self.the_image)
    ratio_height = (890*self.image_height)/self.image_width     
    size = (890,ratio_height)
    image = image.resize(size, Image.ANTIALIAS)
    image.save(self.the_image.path)

The image is resized and writed to my media folder, but the problem is that the registries isnt saved on the database.

I don´t know what i am missing.


toad013 helped me and i could save the registries to the database but then i got two images being writing to my media folder, one as original and another resized with this code:

def save(self):
    if not self.id and not self.the_image:
        return;

    image = Image.open(self.the_image)
    ratio_height = (890*self.image_height)/self.image_width     
    size = (890,ratio_height)
    image = image.resize(size, Image.ANTIALIAS)
    image.save(self.the_image.path)     
    super(Categorias, self).save()
Renato Prado
  • 3,770
  • 4
  • 21
  • 26

1 Answers1

0

you should still need to call the original save function to save the object

super(Categorias, self).save()
toad013
  • 1,350
  • 9
  • 10
  • Now the registries is being saved to the database, but now i have the original image being saved with the original width and height and the resize version. Is possible just have the resized version? – Renato Prado Oct 15 '13 at 00:40
  • hmmm remove the image.save(...) and make sure the "super" line is at the end and before the super just assign the new resized image you made: "self.image = image.resize(size, Image.ANTIALIAS)" . This way you let django do all the saving and updating of the fields – toad013 Oct 15 '13 at 00:42
  • Getting: TypeError at /admin/imagens/categorias/add/ 'tuple' object is not callable Exception Value: 'tuple' object is not callable def save(self): if not self.id and not self.the_image: return; image = Image.open(self.the_image) ratio_height = (890*self.image_height)/self.image_width size = (890,ratio_height) image = image.resize(size, Image.ANTIALIAS) #image.save(self.the_image.path) self.image = image.size(size, Image.ANTIALIAS) super(Categorias, self).save() – Renato Prado Oct 15 '13 at 00:59
  • image.resize not image.size ... and self.the_image (sorry typo on my part) basically your assigning the resized image from "image.resize" to self.the_image and then just have django save the object – toad013 Oct 15 '13 at 01:01
  • Getting: Exception Type: AttributeError Exception Value: width – Renato Prado Oct 15 '13 at 01:15
  • I did self.the_image = image.resize(size, Image.ANTIALIAS) – Renato Prado Oct 15 '13 at 01:15
  • actually, yeah you can't do that because PIL returns an Image object and django doesn't know what that is... this is different from your original question though... you might want to look here: http://stackoverflow.com/questions/6359880/save-image-created-via-pil-to-django-model – toad013 Oct 15 '13 at 01:25