Thumbnails: Do not convert JPG to PNG images

This commit is contained in:
Raphael Michel
2021-06-28 09:18:02 +02:00
parent 6da8caaa2b
commit 93c791e16f

View File

@@ -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)