Scrapy: Image Background converting to green.

Created on 15 Dec 2016  路  4Comments  路  Source: scrapy/scrapy

Hi,

Problem is I'm downloading images with crawler but they are transparency. So image is looking like this.

a2062c7f64b9a136c16f1a3d8491b70902986fc4

But it should look like this.
##
wd-bvbz0120jch-12tb-my-cloud-ex2-ultra-gigabit-ethernet-kisisel-bulut-depolama

bug

Most helpful comment

I had the exact same problem, for us it was a PNG in palette mode. You have to convert it to a RGBA, make the background white and then convert that image to RGB.
For a quick fix you can inherit ImagesPipeline and override the convert_image method with

class CustomImagesPipeline(ImagesPipeline):
    def convert_image(self, image, size=None):
            if image.format == 'PNG' and image.mode == 'RGBA':
                background = Image.new('RGBA', image.size, (255, 255, 255))
                background.paste(image, image)
                image = background.convert('RGB')
            elif image.mode == 'P': #  Here follows the changes from the original Scrapy 1.3.2 code
                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')

            if size:
                image = image.copy()
                image.thumbnail(size, Image.ANTIALIAS)

            buf = BytesIO()
            image.save(buf, 'JPEG')
            return image, buf

Tested with Scarpy 1.3.2

All 4 comments

I'm not sure why it becomes green (and not other color), but the reason is that currently scrapy converts images to jpeg, and jpeg doesn't support transparency. See also: https://github.com/scrapy/scrapy/issues/1705, https://github.com/scrapy/scrapy/issues/1037.

I would like to work on this and implement it as suggested in https://github.com/scrapy/scrapy/issues/1705#issuecomment-234697561

I had the exact same problem, for us it was a PNG in palette mode. You have to convert it to a RGBA, make the background white and then convert that image to RGB.
For a quick fix you can inherit ImagesPipeline and override the convert_image method with

class CustomImagesPipeline(ImagesPipeline):
    def convert_image(self, image, size=None):
            if image.format == 'PNG' and image.mode == 'RGBA':
                background = Image.new('RGBA', image.size, (255, 255, 255))
                background.paste(image, image)
                image = background.convert('RGB')
            elif image.mode == 'P': #  Here follows the changes from the original Scrapy 1.3.2 code
                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')

            if size:
                image = image.copy()
                image.thumbnail(size, Image.ANTIALIAS)

            buf = BytesIO()
            image.save(buf, 'JPEG')
            return image, buf

Tested with Scarpy 1.3.2

Fixed by #2675

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Hecate2 picture Hecate2  路  3Comments

mohmad-null picture mohmad-null  路  3Comments

Shakyamuni177te picture Shakyamuni177te  路  3Comments

osmenia picture osmenia  路  3Comments

yashrsharma44 picture yashrsharma44  路  4Comments