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
3 changes: 2 additions & 1 deletion Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def test_p_alpha(tmp_path: Path) -> None:
outfile = tmp_path / "temp.pdf"
with Image.open("Tests/images/pil123p.png") as im:
assert im.mode == "P"
assert isinstance(im.info["transparency"], bytes)
assert im.palette is not None
assert im.palette.mode == "RGBA"

# Act
im.save(outfile)
Expand Down
15 changes: 9 additions & 6 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,17 @@ def test_load_transparent_rgb(self) -> None:
def test_save_p_transparent_palette(self, tmp_path: Path) -> None:
in_file = "Tests/images/pil123p.png"
with Image.open(in_file) as im:
# 'transparency' contains a byte string with the opacity for
# each palette entry
assert len(im.info["transparency"]) == 256
assert im.palette is not None
assert im.palette.mode == "RGBA"
assert len(im.palette.palette) == 256 * 4

test_file = tmp_path / "temp.png"
im.save(test_file)

# check if saved image contains same transparency
with Image.open(test_file) as im:
assert len(im.info["transparency"]) == 256
assert im.palette is not None
assert im.palette.mode == "RGBA"
assert len(im.palette.palette) == 256 * 4

assert_image(im, "P", (162, 150))

Expand Down Expand Up @@ -342,7 +343,9 @@ def test_save_p_transparent_black(self, tmp_path: Path) -> None:

# check if saved image contains same transparency
with Image.open(test_file) as im:
assert len(im.info["transparency"]) == 256
assert im.palette is not None
assert im.palette.mode == "RGBA"
assert len(im.palette.palette) == 256 * 4
assert_image(im, "P", (10, 10))
im = im.convert("RGBA")
assert_image(im, "RGBA", (10, 10))
Expand Down
16 changes: 9 additions & 7 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,13 +1253,15 @@ def test_apply_transparency(self) -> None:
assert im.palette.colors == {(0, 0, 0, 0): 0, (1, 1, 1, 128): 1}

# Test that transparency bytes are applied
with Image.open("Tests/images/pil123p.png") as im:
assert isinstance(im.info["transparency"], bytes)
assert im.palette is not None
assert im.palette.colors[(27, 35, 6)] == 24
im.apply_transparency()
assert im.palette is not None
assert im.palette.colors[(27, 35, 6, 214)] == 24
im = Image.new("P", (1, 1))
im.putpalette((0, 0, 0, 1, 1, 1))
assert im.palette is not None
assert im.palette.colors == {(0, 0, 0): 0, (1, 1, 1): 1}

im.info["transparency"] = b"\x00\xff"
im.apply_transparency()
assert im.palette is not None
assert im.palette.colors == {(0, 0, 0, 0): 0, (1, 1, 1, 255): 1}

def test_merge_pa(self) -> None:
p = hopper("P")
Expand Down
18 changes: 14 additions & 4 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,17 @@ def __arrow_c_array__(

def __getstate__(self) -> list[Any]:
im_data = self.tobytes() # load image first
return [self.info, self.mode, self.size, self.getpalette(), im_data]
palette = None
if self.palette:
if (
self.palette.mode == "RGBA"
and self.palette.rawmode is None
and len(self.palette.palette) > 768
):
palette = self.palette.palette
else:
palette = self.getpalette()
return [self.info, self.mode, self.size, palette, im_data]

def __setstate__(self, state: list[Any]) -> None:
Image.__init__(self)
Expand All @@ -836,8 +846,8 @@ def __setstate__(self, state: list[Any]) -> None:
self._mode = mode
self._size = size
self.im = core.new(mode, size)
if mode in ("L", "LA", "P", "PA") and palette:
self.putpalette(palette)
if mode in {"L", "LA", "P", "PA"} and palette:
self.putpalette(palette, "RGBA" if len(palette) > 768 else "RGB")
self.frombytes(data)

def tobytes(self, encoder_name: str = "raw", *args: Any) -> bytes:
Expand Down Expand Up @@ -1766,7 +1776,7 @@ def has_transparency_data(self) -> bool:
:returns: A boolean.
"""
if (
self.mode in ("LA", "La", "PA", "RGBA", "RGBa")
self.mode in {"LA", "La", "PA", "RGBA", "RGBa"}
or "transparency" in self.info
):
return True
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PdfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _write_image(
]
procset = "ImageI" # indexed color

if "transparency" in im.info:
if im.has_transparency_data:
smask = im.convert("LA").getchannel("A")
smask.encoderinfo = {}

Expand Down
12 changes: 8 additions & 4 deletions src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,13 @@ def chunk_tRNS(self, pos: int, length: int) -> bytes:
i = s.find(b"\0")
if i >= 0:
self.im_info["transparency"] = i
else:
elif self.im_palette:
# otherwise, we have a byte string with one alpha value
# for each palette entry
self.im_info["transparency"] = s
self.im_palette = "RGBA", b"".join(
self.im_palette[1][i * 3 : i * 3 + 3] + s[i : i + 1]
for i in range(256)
)
elif self.im_mode in ("1", "L", "I;16", "RGB"):
# 2 bytes for greyscale, 6 for truecolour
if length < (6 if self.im_mode == "RGB" else 2):
Expand Down Expand Up @@ -865,8 +868,9 @@ def _open(self) -> None:
self.default_image = self.info.get("default_image", False)

if self.png.im_palette:
rawmode, data = self.png.im_palette
self.palette = ImagePalette.raw(rawmode, data)
mode, data = self.png.im_palette
self.palette = ImagePalette.ImagePalette(mode, data)
self.palette.dirty = 1

if cid == b"fdAT":
self.__prepare_idat = length - 4
Expand Down
Loading