From 93c791e16f01c576cf2a7c5e82cafc6818b961ae Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Mon, 28 Jun 2021 09:18:02 +0200 Subject: [PATCH] Thumbnails: Do not convert JPG to PNG images --- src/pretix/helpers/thumb.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/pretix/helpers/thumb.py b/src/pretix/helpers/thumb.py index 97291235f..92314b846 100644 --- a/src/pretix/helpers/thumb.py +++ b/src/pretix/helpers/thumb.py @@ -85,12 +85,20 @@ def create_thumbnail(sourcename, size): if crop: image = image.crop(crop) + if source.name.endswith('.jpg') or source.name.endswith('.jpeg'): + # Yields better file sizes for photos + target_ext = 'jpeg' + quality = 95 + else: + target_ext = 'png' + quality = None + checksum = hashlib.md5(image.tobytes()).hexdigest() - name = checksum + '.' + size.replace('^', 'c') + '.png' + name = checksum + '.' + size.replace('^', 'c') + '.' + target_ext buffer = BytesIO() if image.mode not in ("1", "L", "RGB", "RGBA"): image = image.convert('RGB') - image.save(fp=buffer, format='PNG') + image.save(fp=buffer, format=target_ext.upper(), quality=quality) imgfile = ContentFile(buffer.getvalue()) t = Thumbnail.objects.create(source=sourcename, size=size)