Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,12 @@ def test_apply_transparency(self) -> None:
assert im.palette is not None
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:
for enum in (
Image.Transpose,
Expand Down
21 changes: 14 additions & 7 deletions Tests/test_image_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ def test_opaque() -> None:
assert_image_equal(alpha, solid)


def test_rgba() -> None:
with Image.open("Tests/images/transparent.png") as im:
assert im.mode == "RGBA"

assert_image_similar(im.convert("RGBa").convert("RGB"), im.convert("RGB"), 1.5)


def test_rgba_p() -> None:
im = hopper("RGBA")
im.putalpha(hopper("L"))
Expand All @@ -107,6 +114,13 @@ def test_rgba_p() -> None:
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_pa() -> None:
im = hopper().convert("PA")

Expand All @@ -115,13 +129,6 @@ def test_pa() -> None:
assert palette.colors != {}


def test_rgba() -> None:
with Image.open("Tests/images/transparent.png") as im:
assert im.mode == "RGBA"

assert_image_similar(im.convert("RGBa").convert("RGB"), im.convert("RGB"), 1.5)


def test_trns_p(tmp_path: Path) -> None:
im = hopper("P")
im.info["transparency"] = 0
Expand Down
10 changes: 8 additions & 2 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,14 @@ def convert_transparency(
new_im.info["transparency"] = transparency
return new_im

if mode == "P" and self.mode == "RGBA":
return self.quantize(colors)
if self.mode == "RGBA":
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
delete_trns = False
Expand Down
7 changes: 6 additions & 1 deletion src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -2419,7 +2419,12 @@ _merge(PyObject *self, PyObject *args) {
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 *
Expand Down
Loading