Skip to content

Commit 11fbdee

Browse files
committed
image: fix export of paletted PNGs to jpeg
* Fix rendering of some PNGs that could not be converted to jpeg. Paletted pngs can't be directly converted to jpeg, they need to be converted to RGB mode first. The conversion can be ugly, so a better approach will need to be found, but this will do for now.
1 parent 6389271 commit 11fbdee

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

mfr/extensions/image/export.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ def export(self):
4444
image = image.resize((round(image.size[0] * ratio),
4545
round(image.size[1] * ratio)), Image.ANTIALIAS)
4646

47+
# Mode 'P' is for paletted images. They must be converted to RGB before exporting to
48+
# jpeg, otherwise Pillow will throw an error. This is a temporary workaround, as the
49+
# conversion is imprecise and can be ugly.
50+
# See: https://stackoverflow.com/q/21669657
51+
if image.mode == 'P':
52+
image = image.convert('RGB')
53+
4754
# handle transparency
4855
# from https://github.com/python-pillow/Pillow/issues/2609
4956
if image.mode in ('RGBA', 'RGBa', 'LA') and type in ['jpeg', 'jpg']:

0 commit comments

Comments
 (0)