From ee5de25f8791659fdf43110404dda8908a6a79f7 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 5 Sep 2022 11:58:45 +1000 Subject: [PATCH] Apply transparency to P images before passing to tkinter.PhotoImage --- Tests/test_imagetk.py | 7 +++++++ src/PIL/ImageTk.py | 1 + 2 files changed, 8 insertions(+) diff --git a/Tests/test_imagetk.py b/Tests/test_imagetk.py index a929910b3..a848c786f 100644 --- a/Tests/test_imagetk.py +++ b/Tests/test_imagetk.py @@ -69,6 +69,13 @@ def test_photoimage(): assert_image_equal(reloaded, im.convert("RGBA")) +def test_photoimage_apply_transparency(): + with Image.open("Tests/images/pil123p.png") as im: + im_tk = ImageTk.PhotoImage(im) + reloaded = ImageTk.getimage(im_tk) + assert_image_equal(reloaded, im.convert("RGBA")) + + def test_photoimage_blank(): # test a image using mode/size: for mode in TK_MODES: diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index 7c90a0ad8..949cf1fbf 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -107,6 +107,7 @@ class PhotoImage: mode = image.mode if mode == "P": # palette mapped data + image.apply_transparency() image.load() try: mode = image.palette.mode