I am trying to create and save a small image while saving the original user image in the model userProfilein my project, below is my code:
def save(self, *args, **kwargs):
super(UserProfile, self).save(*args, **kwargs)
THUMB_SIZE = 45, 45
image = 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' + ext
tf = 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)
Everything is working fine, only one thing.
The problem is that the thumbnail function only sets the width to 45and does not change the aspect ratio of the image, so I get the image 45*35for the one I'm testing on (short image).
Can someone tell me what I am doing wrong? How to make the aspect ratio I want?
PS: I tried all the methods for size: tupal: THUMB_SIZE = (45, 45)and also entered the dimensions directly into the thumbnail function.
: PIL? ?