1
from __future__ import annotations
7
from .helper import assert_image_equal, hopper
12
from PIL import ImageTk
16
except (OSError, ImportError):
20
TK_MODES = ("1", "L", "P", "RGB", "RGBA")
23
pytestmark = pytest.mark.skipif(not HAS_TK, reason="Tk not installed")
26
def setup_module() -> None:
31
except RuntimeError as v:
32
pytest.skip(f"RuntimeError: {v}")
33
except tk.TclError as v:
34
pytest.skip(f"TCL Error: {v}")
38
TEST_JPG = "Tests/images/hopper.jpg"
39
TEST_PNG = "Tests/images/hopper.png"
40
with Image.open(TEST_JPG) as im1:
41
with Image.open(TEST_PNG) as im2:
42
with open(TEST_PNG, "rb") as fp:
44
kw = {"file": TEST_JPG, "data": data}
47
im = ImageTk._get_image_from_kw(kw)
49
assert_image_equal(im, im1)
52
im = ImageTk._get_image_from_kw(kw)
54
assert_image_equal(im, im2)
57
im = ImageTk._get_image_from_kw(kw)
61
@pytest.mark.parametrize("mode", TK_MODES)
62
def test_photoimage(mode: str) -> None:
67
im_tk = ImageTk.PhotoImage(im)
69
assert im_tk.width() == im.width
70
assert im_tk.height() == im.height
72
reloaded = ImageTk.getimage(im_tk)
73
assert_image_equal(reloaded, im.convert("RGBA"))
75
with pytest.raises(ValueError):
77
with pytest.raises(ValueError):
78
ImageTk.PhotoImage(mode)
81
def test_photoimage_apply_transparency() -> None:
82
with Image.open("Tests/images/pil123p.png") as im:
83
im_tk = ImageTk.PhotoImage(im)
84
reloaded = ImageTk.getimage(im_tk)
85
assert_image_equal(reloaded, im.convert("RGBA"))
88
@pytest.mark.parametrize("mode", TK_MODES)
89
def test_photoimage_blank(mode: str) -> None:
91
im_tk = ImageTk.PhotoImage(mode, (100, 100))
93
assert im_tk.width() == 100
94
assert im_tk.height() == 100
96
im = Image.new(mode, (100, 100))
97
reloaded = ImageTk.getimage(im_tk)
98
assert_image_equal(reloaded.convert(mode), im)
101
def test_bitmapimage() -> None:
105
im_tk = ImageTk.BitmapImage(im)
107
assert im_tk.width() == im.width
108
assert im_tk.height() == im.height
113
with pytest.raises(ValueError):
114
ImageTk.BitmapImage()