markubiak / wallpaper-reddit

Downloads and sets wallpapers pulled from reddit.com
GNU General Public License v3.0
113 stars 42 forks source link

Fix for pngs with alpha channel: Convert RGBA to RGB by layering image on a black background #53

Closed maltelau closed 5 years ago

maltelau commented 5 years ago

When png images have an alpha layer, they can retain them in RGBA mode even though we ask for RGB mode. This causes img.save(..., "JPEG") to fail with IOError("cannot write mode RGBA as JPEG"). This commit layers RGBA images on top of a black background before removing the alpha channel and converting to RGB.

Example image that used to fail: https://i.redd.it/gnro5oykh1821.png Issue in Pillow: python-pillow/Pillow#2609 StackOverflow solution: http://stackoverflow.com/a/9459208/284318

markubiak commented 5 years ago

Thanks for the PR. I'll test this when I get a chance to do so; I've encountered issues with RGBA images before but was relatively sure I had fixed it.

maltelau commented 5 years ago

No problem :)

Pillow used to just ignore alpha channels when saving to jpeg, but it's been deprecated as a warning since 3.4 and as an error since 4.2 (https://pillow.readthedocs.io/en/4.1.x/releasenotes/3.4.0.html#deprecation-warning-when-saving-jpegs). Current version from the ubuntu package python-pil is 5.1

markubiak commented 5 years ago

Unsure why it didn't credit you, but I did merge your code into v4 work. Thanks for your contribution.