Skip to content

Corrupted alpha channel after palletization #9155

@bryghtlabs-richard

Description

@bryghtlabs-richard

What did you do?

I converted an image from "RGBA" to "PA"

What did you expect to happen?

I expected the source RGB data to be palettized in the destination P channel, and the source A data to be copied to the A channel.

What actually happened?

The P channel data was zeroed, and the A data was replaced with 0xFF.

What are your OS, Python and Pillow versions?

  • OS: Windows 11
  • Python: 3.13.3
  • Pillow: 11.2.1
--------------------------------------------------------------------
Pillow 11.2.1
Python 3.13.3 (tags/v3.13.3:6280bb5, Apr  8 2025, 14:47:33) [MSC v.1943 64 bit (AMD64)]
--------------------------------------------------------------------
Python executable is C:\Python313\python.exe
System Python files loaded from C:\Python313
--------------------------------------------------------------------
Python Pillow modules loaded from C:\Users\RichardAllen\AppData\Roaming\Python\Python313\site-packages\PIL
Binary Pillow modules loaded from C:\Users\RichardAllen\AppData\Roaming\Python\Python313\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.2.1
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.3
--- LITTLECMS2 support ok, loaded 2.17
--- WEBP support ok, loaded 1.5.0
*** AVIF support not installed
--- JPEG support ok, compiled for libjpeg-turbo 3.1.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.3
--- ZLIB (PNG/ZIP) support ok, loaded 1.3.1.zlib-ng, compiled for zlib-ng 2.2.4
--- LIBTIFF support ok, loaded 4.7.0
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
from PIL import Image

#4xRGBA pixels
pixels = bytes([
    0, 1, 2, 3,
    4, 5, 6, 7,
    8, 9, 10, 11,
    12, 13, 14, 15,
    ])

img = Image.frombytes('RGBA', (4, 1), pixels)
print("RGBA data:", list(img.getdata()))
# img = img.convert("PA")#same result as next line
img = img.convert("PA", palette=Image.ADAPTIVE, colors=4)
print("PA data:", list(img.getdata()))
# print("PA pal:", list(img.getpalette(rawmode="RGB")))

Expected

$ python ~/Desktop/rgb2pa.py
RGBA data: [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15)]
PA data: [(0, 3), (1, 7), (2, 11), (3, 15)]

Actual

$ python ~/Desktop/rgb2pa.py
RGBA data: [(0, 1, 2, 3), (4, 5, 6, 7), (8, 9, 10, 11), (12, 13, 14, 15)]
PA data: [(0, 255), (0, 255), (0, 255), (0, 255)]

We know asking good questions takes effort, and we appreciate your time.

Thank you for Pillow and your support too!

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