From 21d794d35ae1347918e6bf5b2ffe13515ea28795 Mon Sep 17 00:00:00 2001 From: Simon Diviani Gartz Date: Tue, 21 Mar 2017 17:04:30 +0100 Subject: [PATCH] Fixes conversion of transparent PNG with palette images to jpg #2452 --- scrapy/pipelines/images.py | 5 +++++ tests/test_pipeline_images.py | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/scrapy/pipelines/images.py b/scrapy/pipelines/images.py index 5796bfb80..bc449431f 100644 --- a/scrapy/pipelines/images.py +++ b/scrapy/pipelines/images.py @@ -132,6 +132,11 @@ class ImagesPipeline(FilesPipeline): background = Image.new('RGBA', image.size, (255, 255, 255)) background.paste(image, image) image = background.convert('RGB') + elif image.mode == 'P': + image = image.convert("RGBA") + background = Image.new('RGBA', image.size, (255, 255, 255)) + background.paste(image, image) + image = background.convert('RGB') elif image.mode != 'RGB': image = image.convert('RGB') diff --git a/tests/test_pipeline_images.py b/tests/test_pipeline_images.py index 342f25ea9..0f3047602 100644 --- a/tests/test_pipeline_images.py +++ b/tests/test_pipeline_images.py @@ -96,6 +96,14 @@ class ImagesPipelineTestCase(unittest.TestCase): self.assertEquals(converted.mode, 'RGB') self.assertEquals(converted.getcolors(), [(10000, (205, 230, 255))]) + # transparency case with palette: P and PNG + COLOUR = (0, 127, 255, 50) + im = _create_image('PNG', 'RGBA', SIZE, COLOUR) + im = im.convert('P') + converted, _ = self.pipeline.convert_image(im) + self.assertEquals(converted.mode, 'RGB') + self.assertEquals(converted.getcolors(), [(10000, (205, 230, 255))]) + class DeprecatedImagesPipeline(ImagesPipeline): def file_key(self, url):