From 0a8c113630200c03722af1d42c48671767b1a6ae Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 24 Apr 2022 19:19:01 +0200 Subject: [PATCH] Resize images when saving --- src/purchase/models.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/purchase/models.py b/src/purchase/models.py index 3441656..ba2ac4d 100644 --- a/src/purchase/models.py +++ b/src/purchase/models.py @@ -1,5 +1,6 @@ from django.db import models from django.urls import reverse +from PIL import Image, ImageOps class Model(models.Model): @@ -33,6 +34,39 @@ class Product(Model): def __str__(self): return self.name + def save(self, *args, **kwargs): + super().save() + with Image.open(self.image.path) as img: + img = ImageOps.exif_transpose(img) + + width, height = img.size # Get dimensions + + if width > 300 and height > 300: + # keep ratio but shrink down + img.thumbnail((width, height)) + + # check which one is smaller + if height < width: + # make square by cutting off equal amounts left and right + left = (width - height) / 2 + right = (width + height) / 2 + top = 0 + bottom = height + img = img.crop((left, top, right, bottom)) + + elif width < height: + # make square by cutting off bottom + left = 0 + right = width + top = 0 + bottom = width + img = img.crop((left, top, right, bottom)) + + if width > 300 and height > 300: + img.thumbnail((300, 300)) + + img.save(self.image.path) + class Basket(Model): payment_method = models.ForeignKey(