Corrected Palm P mode save by radarhere · Pull Request #8685 · python-pillow/Pillow

Removes an xfail

@pytest.mark.xfail(reason="Palm P image is wrong")
def test_p_mode(tmp_path: Path) -> None:

with the following changes

  1. Always save the colormap for a P mode image, because
    if im.mode == "P":
    # we assume this is a color Palm image with the standard colormap,
    # unless the "info" dict has a "custom-colormap" field

    is an odd assumption.
  2. Use OR instead of AND to apply the custom colormap flag
    flags = flags & _FLAGS["custom-colormap"]
  3. Do not presume that palettes are 256 in length.

At first glance, you might think that

elif colormapmode == "RGBA":
fp.write(
o8(colormap[4 * i])
+ o8(colormap[4 * i + 1])
+ o8(colormap[4 * i + 2])
)

offered support for saving RGBA palettes. But if you look again, you will see that is only writing three values, meaning it's not writing RGBA palettes, it's converting them to RGB. However, getpalette() will only return a RGB palette, so we won't need to worry about handling that.