fdintino / pillow-avif-plugin

A pillow plugin that adds avif support via libavif
BSD 2-Clause "Simplified" License
77 stars 12 forks source link

Opacity information is lost when reading a P mode image with transparency information #48

Open alexwlchan opened 2 months ago

alexwlchan commented 2 months ago

I spotted an instance where pillow_avif is losing transparency information about PNG images with mode P and a custom transparency value.

Here's a sample program that reproduces the issue:

from PIL import Image, ImageDraw
import pillow_avif

# Draw a basic checkerboard image
im = Image.new("P", size=(100, 100))

draw = ImageDraw.Draw(im)
draw.rectangle(xy=[(0, 0),   (50, 50)],   fill=255)
draw.rectangle(xy=[(50, 50), (100, 100)], fill=255)

# Save the image as a PNG, marking the 0'th colour in the palette as transparent
im.save("checkerboard.png", transparency=0)

# Open the image, then save it as an AVIF
opened_im = Image.open("checkerboard.png")
opened_im.save("checkerboard.avif")

This is how the two images get rendered on my Mac. Notice that the PNG correctly has two transparent regions, whereas the AVIF is solid black:

Screenshot 2024-04-29 at 22 34 43

I'm using pillow==10.3.0 and pillow-avif-plugin==1.4.3.

alexwlchan commented 2 months ago

Note: this may have a similar underlying cause/fix to https://github.com/bigcat88/pillow_heif/issues/235, which I spotted while writing this issue.