Skip to content

Broken quantized png #9128

Closed
Closed

Description

@imfantuan

What did you do?

from PIL import Image
img = Image.open('plusx.jpg')
print(f"Original size: {img.size}, mode: {img.mode}")
resized = img.resize((img.width//2, img.height//2), Image.Resampling.LANCZOS)
quantized = resized.quantize(colors=256, method=3)
print(f"Quantized mode: {quantized.mode}, palette colors: {len(quantized.getpalette())//3}")
quantized.save('output_quantized.png')
print("Saved: output_quantized.png")

What did you expect to happen?

saved a nice quantized png

What actually happened?

saved a broken quantized png

What are your OS, Python and Pillow versions?

  • OS: macOS 26 Public Beta
  • Python: python@3.13
  • Pillow: 11.3
/opt/homebrew/Cellar/python@3.13/3.13.3_1/bin/python3  -m PIL --report
--------------------------------------------------------------------
Pillow 11.3.0
Python 3.13.3 (main, Apr  8 2025, 13:54:08) [Clang 17.0.0 (clang-1700.0.13.3)]
--------------------------------------------------------------------
Python executable is /opt/homebrew/opt/python@3.13/bin/python3.13
System Python files loaded from /opt/homebrew/opt/python@3.13/Frameworks/Python.framework/Versions/3.13
--------------------------------------------------------------------
Python Pillow modules loaded from /opt/homebrew/lib/python3.13/site-packages/PIL
Binary Pillow modules loaded from /opt/homebrew/lib/python3.13/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.3.0
*** TKINTER support not installed
--- FREETYPE2 support ok, loaded 2.13.3
--- LITTLECMS2 support ok, loaded 2.17
--- WEBP support ok, loaded 1.5.0
--- AVIF support ok, loaded 1.3.0
--- JPEG support ok, compiled for libjpeg-turbo 3.1.1
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.3
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.12
--- LIBTIFF support ok, loaded 4.7.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.2
--- LIBIMAGEQUANT (Quantization method) support ok, loaded 4.2.2
--- XCB (X protocol) support ok

Origin Jpeg:

https://demopub.s3.us-west-000.backblazeb2.com/plusx.jpg

Output Png:

Image

Activity

  1. radarhere commented on Aug 3, 2025

    @radarhere
    Member

    Interesting. While this is something worth investigating and solving, if you're looking for an immediate solution, I will point out that the problem doesn't occur without the resize operation. So you could quantize the image first, and then resize.

  2. imfantuan commented on Aug 3, 2025

    @imfantuan
    Author

    Thank you sir.

  3. imfantuan commented on Aug 3, 2025

    @imfantuan
    Author

    I found that quantizing first and then resizing will get a larger image, but it's still smaller than not quantizing at all.

  4. radarhere commented on Aug 5, 2025

    @radarhere
    Member

    I've created #9133 to resolve this.

    For an immediate solution, I suggest converting to RGBA before quantizing.

    from PIL import Image
    img = Image.open('plusx.jpg')
    print(f"Original size: {img.size}, mode: {img.mode}")
    resized = img.resize((img.width//2, img.height//2), Image.Resampling.LANCZOS)
    quantized = resized.convert("RGBA").quantize(colors=256, method=3)
    print(f"Quantized mode: {quantized.mode}, palette colors: {len(quantized.getpalette())//3}")
    quantized.save('output_quantized.png')
    print("Saved: output_quantized.png")
  5. imfantuan commented on Aug 5, 2025

    @imfantuan
    Author

    It works perfect now. THX!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions