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

Fixed bug with rounding pixels to palette colors #6377

Merged
merged 5 commits into from Jun 22, 2022

Conversation

btrekkie
Copy link
Contributor

This fixes a bug with rounding pixels to the nearest palette color. Specifically, there is a typo with computing the minimum distance from a palette color to a given cache box. This is causing palette colors to be incorrectly excluded from consideration.

The bug can be observed in the following example:

from PIL import Image
palette = Image.new('P', (1, 1))
palette.putpalette([0, 0, 0, 32, 32, 32] + ([0, 0, 0] * 254))
image = Image.new('L', (9, 1))
image.putdata([0, 4, 8, 12, 16, 20, 24, 28, 32])
quantized_image = image.convert('RGB').quantize(
    dither=Image.Dither.NONE, palette=palette)
print(list(quantized_image.convert('L').getdata()))

Before the change, this prints [0, 0, 0, 0, 0, 0, 0, 0, 32]. After the change, it prints [0, 0, 0, 0, 0, 32, 32, 32, 32].

Changes proposed in this pull request:

  • Fix bug with rounding pixels to palette.
  • Update tests to account for fix.

This fixes a bug with rounding pixels to the nearest palette color. Specifically, there is a typo with computing the minimum distance from a palette color to a given cache box. This is causing palette colors to be incorrectly excluded from consideration.
@hugovk hugovk merged commit 6dd5b2e into python-pillow:main Jun 22, 2022
@radarhere radarhere changed the title Fixed bug with rounding pixels to palette Fixed bug with rounding pixels to palette colors Jun 22, 2022
@btrekkie btrekkie deleted the fix-round-to-palette branch June 22, 2022 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants