I am trying to create and save a thumbnail image when saving the original user image in the userProfile
model in my project, below is my code:
def save(self, *args, **kwargs):super(UserProfile, self).save(*args, **kwargs)THUMB_SIZE = 45, 45image = Image.open(join(MEDIA_ROOT, self.headshot.name))fn, ext = os.path.splitext(self.headshot.name)image.thumbnail(THUMB_SIZE, Image.ANTIALIAS) thumb_fn = fn + '-thumb' + exttf = NamedTemporaryFile()image.save(tf.name, 'JPEG')self.headshot_thumb.save(thumb_fn, File(open(tf.name)), save=False)tf.close()super(UserProfile, self).save(*args, **kwargs)
Every thing is working OK, just this one thing.
The problem is that the thumbnail function only sets the width to 45
and doesn't change the ratio aspect of the image so I am getting an image of 45*35
for the one that I am testing on (short image).
Can any one tell me what am I doing wrong? How to force the aspect ratio I want?
P.S.: I've tried all the methods for size: tupal: THUMB_SIZE = (45, 45)
and entering the sizes directly to the thumbnail function also.
Another question: what is the deference between resize and thumbnail functions in PIL? When to use resize and when to use thumbnail?