This commit is contained in:
Andrew Murray 2025-09-07 11:56:23 +02:00 committed by GitHub
commit 67c05732f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 3 deletions

View File

@ -1126,6 +1126,12 @@ class TestImage:
assert im.palette is not None assert im.palette is not None
assert im.palette.colors[(27, 35, 6, 214)] == 24 assert im.palette.colors[(27, 35, 6, 214)] == 24
def test_merge_pa(self) -> None:
p = hopper("P")
a = Image.new("L", p.size)
pa = Image.merge("PA", (p, a))
assert p.getpalette() == pa.getpalette()
def test_constants(self) -> None: def test_constants(self) -> None:
for enum in ( for enum in (
Image.Transpose, Image.Transpose,

View File

@ -107,6 +107,13 @@ def test_rgba_p() -> None:
assert_image_similar(im, comparable, 20) assert_image_similar(im, comparable, 20)
def test_rgba_pa() -> None:
im = hopper("RGBA").convert("PA").convert("RGB")
expected = hopper("RGB")
assert_image_similar(im, expected, 9.3)
def test_rgba() -> None: def test_rgba() -> None:
with Image.open("Tests/images/transparent.png") as im: with Image.open("Tests/images/transparent.png") as im:
assert im.mode == "RGBA" assert im.mode == "RGBA"

View File

@ -1009,8 +1009,14 @@ class Image:
new_im.info["transparency"] = transparency new_im.info["transparency"] = transparency
return new_im return new_im
if mode == "P" and self.mode == "RGBA": if self.mode == "RGBA":
return self.quantize(colors) if mode == "P":
return self.quantize(colors)
elif mode == "PA":
r, g, b, a = self.split()
rgb = merge("RGB", (r, g, b))
p = rgb.quantize(colors)
return merge("PA", (p, a))
trns = None trns = None
delete_trns = False delete_trns = False

View File

@ -2419,7 +2419,12 @@ _merge(PyObject *self, PyObject *args) {
bands[3] = band3->image; bands[3] = band3->image;
} }
return PyImagingNew(ImagingMerge(mode, bands)); Imaging imOut = ImagingMerge(mode, bands);
if (!imOut) {
return NULL;
}
ImagingCopyPalette(imOut, bands[0]);
return PyImagingNew(imOut);
} }
static PyObject * static PyObject *