From 98711619ff82724aa7e7db07023ac990f9a1552f Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Tue, 8 Jun 2021 22:39:45 +0200 Subject: [PATCH] Create image_processing.py Added central function to rescale and compress images for recipes (or in general). Switched from previously used PNG format to 75% JPEG format for a 5-10x file size reduction with hardly any quality loss. --- cookbook/helper/image_processing.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 cookbook/helper/image_processing.py diff --git a/cookbook/helper/image_processing.py b/cookbook/helper/image_processing.py new file mode 100644 index 000000000..bf84070fa --- /dev/null +++ b/cookbook/helper/image_processing.py @@ -0,0 +1,15 @@ +from PIL import Image +from io import BytesIO + + +def rescale_image(image_object, base_width=720): + img = Image.open(image_object) + icc_profile = img.info.get('icc_profile') # remember color profile to not mess up colors + width_percent = (base_width / float(img.size[0])) + height = int((float(img.size[1]) * float(width_percent))) + + img = img.resize((base_width, height), Image.ANTIALIAS) + img_bytes = BytesIO() + img.save(img_bytes, 'JPEG', quality=75, optimize=True, icc_profile=icc_profile) + + return img_bytes