2020-02-12 19:29:19 +03:00
|
|
|
import pytest
|
2020-09-01 20:16:46 +03:00
|
|
|
|
2016-05-01 15:19:52 +03:00
|
|
|
from PIL import Image
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-10-05 00:48:23 +03:00
|
|
|
from .helper import assert_image_equal, hopper
|
2017-01-24 16:43:58 +03:00
|
|
|
|
2016-05-01 15:19:52 +03:00
|
|
|
try:
|
2019-09-26 15:12:28 +03:00
|
|
|
import tkinter as tk
|
|
|
|
|
2020-09-01 20:16:46 +03:00
|
|
|
from PIL import ImageTk
|
|
|
|
|
2016-05-01 15:19:52 +03:00
|
|
|
dir(ImageTk)
|
2017-01-24 16:43:58 +03:00
|
|
|
HAS_TK = True
|
2018-10-01 13:22:18 +03:00
|
|
|
except (OSError, ImportError):
|
2020-02-12 19:29:19 +03:00
|
|
|
# Skipped via pytestmark
|
2017-01-24 16:43:58 +03:00
|
|
|
HAS_TK = False
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2019-06-13 18:54:46 +03:00
|
|
|
TK_MODES = ("1", "L", "P", "RGB", "RGBA")
|
2016-08-04 09:40:12 +03:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
pytestmark = pytest.mark.skipif(not HAS_TK, reason="Tk not installed")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def setup_module():
|
|
|
|
try:
|
|
|
|
# setup tk
|
|
|
|
tk.Frame()
|
|
|
|
# root = tk.Tk()
|
2022-05-04 15:30:44 +03:00
|
|
|
except RuntimeError as v:
|
|
|
|
pytest.skip(f"RuntimeError: {v}")
|
2020-02-12 19:29:19 +03:00
|
|
|
except tk.TclError as v:
|
2020-07-16 12:43:29 +03:00
|
|
|
pytest.skip(f"TCL Error: {v}")
|
2019-11-25 23:03:23 +03:00
|
|
|
|
2016-05-01 15:19:52 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_kw():
|
|
|
|
TEST_JPG = "Tests/images/hopper.jpg"
|
|
|
|
TEST_PNG = "Tests/images/hopper.png"
|
|
|
|
with Image.open(TEST_JPG) as im1:
|
|
|
|
with Image.open(TEST_PNG) as im2:
|
|
|
|
with open(TEST_PNG, "rb") as fp:
|
|
|
|
data = fp.read()
|
|
|
|
kw = {"file": TEST_JPG, "data": data}
|
2016-05-01 15:19:52 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test "file"
|
|
|
|
im = ImageTk._get_image_from_kw(kw)
|
|
|
|
assert_image_equal(im, im1)
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test "data"
|
|
|
|
im = ImageTk._get_image_from_kw(kw)
|
|
|
|
assert_image_equal(im, im2)
|
2017-01-24 16:43:58 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test no relevant entry
|
|
|
|
im = ImageTk._get_image_from_kw(kw)
|
|
|
|
assert im is None
|
2017-01-24 16:43:58 +03:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_photoimage():
|
|
|
|
for mode in TK_MODES:
|
|
|
|
# test as image:
|
|
|
|
im = hopper(mode)
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# this should not crash
|
|
|
|
im_tk = ImageTk.PhotoImage(im)
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
assert im_tk.width() == im.width
|
|
|
|
assert im_tk.height() == im.height
|
2017-01-24 16:43:58 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
reloaded = ImageTk.getimage(im_tk)
|
|
|
|
assert_image_equal(reloaded, im.convert("RGBA"))
|
2017-01-24 16:43:58 +03:00
|
|
|
|
2017-04-20 14:14:23 +03:00
|
|
|
|
2022-09-05 04:58:45 +03:00
|
|
|
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"))
|
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_photoimage_blank():
|
|
|
|
# test a image using mode/size:
|
|
|
|
for mode in TK_MODES:
|
|
|
|
im_tk = ImageTk.PhotoImage(mode, (100, 100))
|
|
|
|
|
|
|
|
assert im_tk.width() == 100
|
|
|
|
assert im_tk.height() == 100
|
2017-01-24 16:43:58 +03:00
|
|
|
|
2022-02-07 11:15:25 +03:00
|
|
|
im = Image.new(mode, (100, 100))
|
|
|
|
reloaded = ImageTk.getimage(im_tk)
|
|
|
|
assert_image_equal(reloaded.convert(mode), im)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2022-04-02 15:44:28 +03:00
|
|
|
def test_box_deprecation():
|
|
|
|
im = hopper()
|
|
|
|
im_tk = ImageTk.PhotoImage(im)
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
im_tk.paste(im, (0, 0, 128, 128))
|
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
def test_bitmapimage():
|
|
|
|
im = hopper("1")
|
|
|
|
|
|
|
|
# this should not crash
|
|
|
|
im_tk = ImageTk.BitmapImage(im)
|
|
|
|
|
|
|
|
assert im_tk.width() == im.width
|
|
|
|
assert im_tk.height() == im.height
|
|
|
|
|
|
|
|
# reloaded = ImageTk.getimage(im_tk)
|
|
|
|
# assert_image_equal(reloaded, im)
|