Resize image on save (django admin)

646 views
Skip to first unread message

nsash

unread,
Sep 5, 2008, 12:58:35 PM9/5/08
to Django users
Hello,

I am trying to resize image on save from the admin part of django.

My Model is:

class Artwork(models.Model):
file = models.ImageField(upload_to=UPLOAD_DIR,)
title = models.CharField(max_length=100)

def __unicode__(self):
return self.title


def save(self, size=(80, 80)):
super(Artwork, self).save()
if self.file:
filename = self.file
image = Image.open(filename)

image.thumbnail(size, Image.ANTIALIAS)
image.save(filename)


the problem is that I get the error message

'ImageFieldFile' object has no attribute '_mode'.

When I change
filename = self.file
to
filename = '/path/to/some/image/in/the/filesystem/
image.jpg' (hardcoded)
the image is resized without problems.

I think the problem is that I try to open an image that is not yet
stored in the UPLOAD_DIR, but that is only an assumption.

Michel Thadeu Sabchuk

unread,
Sep 5, 2008, 2:13:53 PM9/5/08
to Django users
Hi nsash,

>         def save(self, size=(80, 80)):
>                 super(Artwork, self).save()
>                 if self.file:
>                         filename = self.file
>                         image = Image.open(filename)
>
>                         image.thumbnail(size, Image.ANTIALIAS)
>                         image.save(filename)
>
> the problem is that I get the error message
>
> 'ImageFieldFile' object has no attribute '_mode'.

What django version are you using? Try to change the line "filename =
self.file" to "filename = self.file.path"... The way files are treated
on django changed recently, search for the backward incompatibility
changes to know more.

Best regards!

nsash

unread,
Sep 5, 2008, 2:45:11 PM9/5/08
to Django users
Thank you very much,

this solved the problem
By the way, I use the official release 1.0
Reply all
Reply to author
Forward
0 new messages