Answer the question
In order to leave comments, you need to log in
How to change file size before saving in model?
My code:
class ...(models.Model):
...
def save(self, *args, **kwargs):
super(Photo, self).save(*args, **kwargs)
filepath = self.image.path
width = self.image.width
height = self.image.height
if height < width:
new_width = int(round(297*90/25))
new_height = int(round(210*90/25))
else:
new_height = int(round(297*90/25))
new_width = int(round(210*90/25))
image = Image.open(filepath)
image = image.resize((new_width,new_height), Image.ANTIALIAS)
image.save(filepath, format='JPEG', quality=90)
Answer the question
In order to leave comments, you need to log in
If the action is frequent, I would look towards the fields for the models from django-imagemagick and not fence the bikes
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question