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

PNG palette quantized save functionality different between 8.1.0 and 8.3.1 #5695

Closed
paulds8 opened this issue Aug 30, 2021 · 1 comment · Fixed by #5696
Closed

PNG palette quantized save functionality different between 8.1.0 and 8.3.1 #5695

paulds8 opened this issue Aug 30, 2021 · 1 comment · Fixed by #5696
Labels

Comments

@paulds8
Copy link

paulds8 commented Aug 30, 2021

What did you do?

Create a palette, quantize an image using this palette and save it.

What did you expect to happen?

The palette information is stored correctly. I.e. the list of RGB values is the same in the saved image.
E.g. [0, 0, 0, 209, 254, 76, 217, 3, 3, 10, 66, 192,...]

What actually happened?

The list of RGB values is corrupted.
E.g. [0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3,...]

What are your OS, Python and Pillow versions?

  • OS: Tested on Windows and Azure Databricks (Ubuntu)
  • Python: 3.6 & 3.8
  • Pillow: 8.3.1 has the issue
from PIL import Image
from itertools import cycle
import numpy as np

palette = [0, 0, 0, 209, 254, 76, 217, 3, 3, 10, 66, 192]
# palette must contain 768 values (256 * 3)
# repeat the colours until we get there
color_iter = cycle(palette)
expanded_palette_format = []
while True:
    expanded_palette_format.append(next(color_iter))
    if len(expanded_palette_format) == 768:
        break

assert len(expanded_palette_format) == 768
palette_image = Image.new('P', (16, 16))
palette_image.putpalette(expanded_palette_format)

# a new image that we want to apply the palette to
im_to_palette = np.array(
    [
        [[0, 0, 0,],[209, 254, 76], [217, 3, 3], [10, 66, 192]],
        [[0, 0, 0,],[209, 254, 76], [217, 3, 3], [10, 66, 192]],
        [[0, 0, 0,],[209, 254, 76], [217, 3, 3], [10, 66, 192]],
        [[0, 0, 0,],[209, 254, 76], [217, 3, 3], [10, 66, 192]]
    ],
    np.uint8
    )
im = Image.fromarray(im_to_palette).quantize(palette=palette_image, dither=0)

im.save(r"test.png")
saved_im = Image.open(r"test.png")

assert im.getpalette() == saved_im.getpalette()
# AssertionError

Please note that if I specifically pass the 'GIF' argument on save, it has the expected behaviour.

im.save(r"test.png", "GIF")
saved_im = Image.open(r"test.png")

assert im.getpalette() == saved_im.getpalette()
@radarhere
Copy link
Member

I've created PR #5696 to resolve this.

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 a pull request may close this issue.

2 participants