From cdaa099a3988b1487b6728215adbf1dc6c2c58fe Mon Sep 17 00:00:00 2001 From: luxagraf Date: Fri, 4 Dec 2020 15:33:33 -0500 Subject: added a function to generate images --- app/media/models.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'app/media') diff --git a/app/media/models.py b/app/media/models.py index a5cacf5..b9437f1 100644 --- a/app/media/models.py +++ b/app/media/models.py @@ -397,3 +397,23 @@ def update_photo_sizes(sender, instance, **kwargs): print("file %s exists" % size) pass +def generate_image(luximage, size): + new_size = LuxImageSize.objects.get(slug=size) + if new_size.width: + img = Image.open(luximage.image.path) + try: + if new_size.width <= img.width: + resize_image(img, new_size.width, None, new_size.quality, luximage.get_image_path_by_size(size)) + else: + raise ValidationError({'items': ["Size is larger than source image"]}) + except ImageSizeError: + print("error creating size") + if new_size.height: + img = Image.open(luximage.image.path) + try: + if new_size.height <= img.height: + resize_image(img, None, new_size.height, new_size.quality, luximage.get_image_path_by_size(size)) + else: + pass + except ImageSizeError: + print("error creating size") -- cgit v1.2.3-70-g09d2