Pillow/Tests/test_imagetk.py

86 lines
2.3 KiB
Python
Raw Normal View History

from PIL import Image
from .helper import PillowTestCase, hopper, unittest
try:
from PIL import ImageTk
2019-06-13 18:54:46 +03:00
2019-09-26 15:12:28 +03:00
import tkinter as tk
dir(ImageTk)
HAS_TK = True
2018-10-01 13:22:18 +03:00
except (OSError, ImportError):
# Skipped via setUp()
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
2018-08-04 20:46:03 +03:00
@unittest.skipIf(not HAS_TK, "Tk not installed")
2014-06-10 13:10:47 +04:00
class TestImageTk(PillowTestCase):
def setUp(self):
2014-06-10 13:10:47 +04:00
try:
# setup tk
tk.Frame()
2017-04-20 14:14:23 +03:00
# root = tk.Tk()
2018-10-02 11:55:28 +03:00
except tk.TclError as v:
self.skipTest("TCL Error: %s" % v)
2014-06-10 13:10:47 +04:00
def test_kw(self):
TEST_JPG = "Tests/images/hopper.jpg"
TEST_PNG = "Tests/images/hopper.png"
im1 = Image.open(TEST_JPG)
im2 = Image.open(TEST_PNG)
2019-06-13 18:54:46 +03:00
with open(TEST_PNG, "rb") as fp:
data = fp.read()
2016-08-04 09:40:12 +03:00
kw = {"file": TEST_JPG, "data": data}
# Test "file"
im = ImageTk._get_image_from_kw(kw)
self.assert_image_equal(im, im1)
# Test "data"
im = ImageTk._get_image_from_kw(kw)
self.assert_image_equal(im, im2)
# Test no relevant entry
im = ImageTk._get_image_from_kw(kw)
self.assertIsNone(im)
def test_photoimage(self):
for mode in TK_MODES:
# test as image:
im = hopper(mode)
2017-04-20 14:14:23 +03:00
# this should not crash
im_tk = ImageTk.PhotoImage(im)
self.assertEqual(im_tk.width(), im.width)
self.assertEqual(im_tk.height(), im.height)
2019-04-29 16:43:13 +03:00
reloaded = ImageTk.getimage(im_tk)
self.assert_image_equal(reloaded, im.convert("RGBA"))
def test_photoimage_blank(self):
# test a image using mode/size:
for mode in TK_MODES:
2017-04-20 14:14:23 +03:00
im_tk = ImageTk.PhotoImage(mode, (100, 100))
self.assertEqual(im_tk.width(), 100)
self.assertEqual(im_tk.height(), 100)
2017-04-20 14:14:23 +03:00
# reloaded = ImageTk.getimage(im_tk)
# self.assert_image_equal(reloaded, im)
def test_bitmapimage(self):
2019-06-13 18:54:46 +03:00
im = hopper("1")
# this should not crash
im_tk = ImageTk.BitmapImage(im)
2017-04-20 14:14:23 +03:00
self.assertEqual(im_tk.width(), im.width)
self.assertEqual(im_tk.height(), im.height)
2017-04-20 14:14:23 +03:00
# reloaded = ImageTk.getimage(im_tk)
# self.assert_image_equal(reloaded, im)