From 3b612062da800e6243ed3ee978ac19a94fd9661a Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Mon, 17 Nov 2014 11:36:19 +0000 Subject: [PATCH] Issue with transparency handling also affects image_data_as_rgb, so make the fix into a shared _to_rgb helper method --- wagtail/wagtailimages/backends/pillow.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/wagtail/wagtailimages/backends/pillow.py b/wagtail/wagtailimages/backends/pillow.py index 6239445791..3f1485a64e 100644 --- a/wagtail/wagtailimages/backends/pillow.py +++ b/wagtail/wagtailimages/backends/pillow.py @@ -16,24 +16,20 @@ class PillowBackend(BaseImageBackend): def save_image(self, image, output, format): image.save(output, format, quality=self.quality) - def resize(self, image, size): - if image.mode in ['1', 'P']: + def _to_rgb(self, image): + if image.mode not in ['RGB', 'RGBA']: if 'transparency' in image.info and isinstance(image.info['transparency'], bytes): image = image.convert('RGBA') else: image = image.convert('RGB') + return image - return image.resize(size, PIL.Image.ANTIALIAS) + def resize(self, image, size): + return self._to_rgb(image).resize(size, PIL.Image.ANTIALIAS) def crop(self, image, rect): return image.crop(rect) def image_data_as_rgb(self, image): - # https://github.com/thumbor/thumbor/blob/f52360dc96eedd9fc914fcf19eaf2358f7e2480c/thumbor/engines/pil.py#L206-L215 - if image.mode not in ['RGB', 'RGBA']: - if 'A' in image.mode: - image = image.convert('RGBA') - else: - image = image.convert('RGB') - + image = self._to_rgb(image) return image.mode, image.tostring()