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
Binary file added Tests/images/tbbn0g04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,17 @@ def test_save_grayscale_transparency(self, tmp_path: Path) -> None:
assert colors is not None
assert colors[0][0] == num_transparent

def test_low_bit_grayscale_transparency(self) -> None:
with Image.open("Tests/images/tbbn0g04.png") as im:
assert im.mode == "L"
assert im.info["transparency"] == 255
assert im.convert("LA").getpixel((0, 0)) == (255, 0)

for bits in (2, 4):
im2 = Image.new("L", (1, 1))
with roundtrip(im2, bits=bits, transparency=255) as reloaded:
assert reloaded.info["transparency"] == 255

def test_save_1_transparency(self, tmp_path: Path) -> None:
out = tmp_path / "temp.png"

Expand Down
7 changes: 6 additions & 1 deletion src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,12 @@ def chunk_tRNS(self, pos: int, length: int) -> bytes:
if self.im_mode == "1":
self.im_info["transparency"] = 255 if i16(s) else 0
elif self.im_mode in ("L", "I;16"):
self.im_info["transparency"] = i16(s)
value = i16(s)
if self.im_rawmode in {"L;2", "L;4"}:
# Scale the grey level to match the unpacked 8-bit pixels
max_value = (1 << int(self.im_rawmode[-1])) - 1
value = int(min(1, value / max_value) * 255)
self.im_info["transparency"] = value
elif self.im_mode == "RGB":
self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4)
return s
Expand Down
Loading