Skip to content

Commit dfd24ba

Browse files
committed
Read all non-zero transparency from mode 1 images in the same way
1 parent e36e670 commit dfd24ba

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Tests/test_file_png.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,15 @@ def test_save_grayscale_transparency(self, tmp_path: Path) -> None:
338338
assert colors is not None
339339
assert colors[0][0] == num_transparent
340340

341+
def test_save_1_transparency(self, tmp_path: Path) -> None:
342+
out = tmp_path / "temp.png"
343+
344+
im = Image.new("1", (1, 1), 1)
345+
im.save(out, transparency=1)
346+
347+
with Image.open(out) as reloaded:
348+
assert reloaded.info["transparency"] == 255
349+
341350
def test_save_rgb_single_transparency(self, tmp_path: Path) -> None:
342351
in_file = "Tests/images/caption_6_33_22.png"
343352
with Image.open(in_file) as im:

src/PIL/PngImagePlugin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,9 @@ def chunk_tRNS(self, pos: int, length: int) -> bytes:
509509
# otherwise, we have a byte string with one alpha value
510510
# for each palette entry
511511
self.im_info["transparency"] = s
512-
elif self.im_mode in ("1", "L", "I;16"):
512+
elif self.im_mode == "1":
513+
self.im_info["transparency"] = 255 if i16(s) else 0
514+
elif self.im_mode in ("L", "I;16"):
513515
self.im_info["transparency"] = i16(s)
514516
elif self.im_mode == "RGB":
515517
self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4)

0 commit comments

Comments
 (0)