override save for imagefield not working

51 views
Skip to first unread message

Sandro

unread,
Sep 10, 2006, 5:26:59 PM9/10/06
to Django users
I am trying to override the save function for my Image class so that
when I upload an image I can generate thumbnails in different folders.
I think my thumbnail code is fine, the problem is that I can't get
information about my image.

def save(self):
super(Image, self).save()
im = Image.open( self.get_image_filename() ) #this fails

It looks like <self> never contains any significant information, before
and after the save.

I tried initializing the last object placed in the database but like
above, this variable has no real information:

def save(self):
super(Image, self).save()
i = Image.objects.order_by('-id')[0]
im = Image.open( i.get_image_filename() )

Is there anyway to get this logic implemented by overriding the save
method or do I have to extend the ImageField class like others
(http://www.verdjn.com/browser/verdjnlib/fields/photofield.py) have?

Sandro

unread,
Sep 10, 2006, 10:29:04 PM9/10/06
to Django users
[530] from IRC worked with me to figure out what's going on.
Apparently save() is getting called twice, the first time saves the
object, the second time applies the the uploaded directory. We need to
check to see if self.image exists before trying to create thumbnails.
Here's a simple version of the script.

imageFolders = {'thumb': (100,75), 'small': (240,180), 'medium':
(500,375)}
super(StoryImage, self).save() # Call the "real" save() method.

if self.image:
im = Image.open(self.get_image_filename())
for dir in imageFolders:
copy = im.copy()
copy.thumbnail(self.imageFolders[dir], Image.ANTIALIAS)
copy.save(self.imagePath+dir+'/'+imagename, 'JPEG', quality=85)

It would probably be smart to remove the old images if you decide to
edit a currently saved image. I am testing to see how this works by
calling os.remove(self.get_image_filename()) prior to calling
super.save()

Any more information on this subject would be helpful.

Reply all
Reply to author
Forward
0 new messages