<commit_before>from __future__ import absolute_import

import PIL.Image

from wagtail.wagtailimages.backends.base import BaseImageBackend


class PillowBackend(BaseImageBackend):
    def __init__(self, params):
        super(PillowBackend, self).__init__(params)

    def open_image(self, input_file):
        image = PIL.Image.open(input_file)
        return image

    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']:
            if 'transparency' in image.info and isinstance(image.info['transparency'], bytes):
                image = image.convert('RGBA')
            else:
                image = image.convert('RGB')

        return 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')

        return image.mode, image.tostring()<commit_msg>Issue with transparency handling also affects image_data_as_rgb, so make the fix into a shared _to_rgb helper method<commit_after>from __future__ import absolute_import

import PIL.Image

from wagtail.wagtailimages.backends.base import BaseImageBackend


class PillowBackend(BaseImageBackend):
    def __init__(self, params):
        super(PillowBackend, self).__init__(params)

    def open_image(self, input_file):
        image = PIL.Image.open(input_file)
        return image

    def save_image(self, image, output, format):
        image.save(output, format, quality=self.quality)

    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

    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):
        image = self._to_rgb(image)
        return image.mode, image.tostring()
{'fraction_non_alphanumeric': 0.10596833130328867, 'fraction_numerical': 0.011774259033698742, 'mean_word_length': 2.8260869565217392, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5438041', 'n_tokens_mistral': 784, 'n_tokens_neox': 751, 'n_words': 175}