mezzanine icon indicating copy to clipboard operation
mezzanine copied to clipboard

Padded thumbnails of JPGs aren't saved correctly

Open molokov opened this issue 6 years ago • 0 comments

#1781 fixed the previous issue where creation of thumbnails would fail for JPGs as they couldn't be converted to RGBA.

However, there's still a problem if the thumbnail will be created with padding, as the new Image is created as RGBA on this line: https://github.com/stephenmcd/mezzanine/blob/cf6c79cd8f1c817416cc210c22232fa6ecc7dd82/mezzanine/core/templatetags/mezzanine_tags.py#L416

Thus, when the file save is tried, with filetype="JPEG" (and a .jpg file extension), this fails, causing the original image to be returned. (On this line: https://github.com/stephenmcd/mezzanine/blob/cf6c79cd8f1c817416cc210c22232fa6ecc7dd82/mezzanine/core/templatetags/mezzanine_tags.py#L425 )

What we should probably do is for JPEGs where the thumbnail has padding is to save them as PNGs.

Possibly this can be done as simply as:

       if pad_size is not None:
            pad_container = Image.new("RGBA", pad_size, padding_color)
            pad_container.paste(image, (pad_left, pad_top))
            image = pad_container
            # Making thumbnail a png
            filetype = "PNG"
            thumb_path += ".png"
            thumb_url += ".png"

It's a little crude, as the thumbnail will have a ".jpg.png" extension, but it's the simplest fix I can think of.

Thoughts?

molokov avatar May 30 '19 01:05 molokov