Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fewer PNG palette entries than the bit depth maximum when saving #5330

Merged
merged 1 commit into from Mar 21, 2021

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented Mar 15, 2021

Resolves #2202

Currently when saving a PNG in P mode, Pillow sets the number of bytes in the palette at two to the power of the bit depth, times 3 for the colors.

if im.mode == "P":
palette_byte_number = (2 ** bits) * 3
palette_bytes = im.im.getpalette("RGB")[:palette_byte_number]

As pointed out in the issue, https://www.w3.org/TR/PNG/#11PLTE states that

It is permissible to have fewer entries than the bit depth would allow.

So this PR changes palette_byte_number to the number of colors times 3 instead.

The length of the tRNS chunk is also changed, as https://www.w3.org/TR/PNG/#11tRNS

The tRNS chunk shall not contain more alpha values than there are palette entries

@hugovk hugovk merged commit ddcc936 into python-pillow:master Mar 21, 2021
50 checks passed
@radarhere radarhere deleted the png_plte branch Mar 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

2 participants