mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-07-07 21:33:28 +03:00
Merge pull request #1856 from radarhere/eafp
Combined duplicate code in ImageTk
This commit is contained in:
commit
42f7a08ca4
|
@ -33,6 +33,7 @@ except ImportError:
|
||||||
del Tkinter
|
del Tkinter
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
@ -52,6 +53,15 @@ def _pilbitmap_check():
|
||||||
_pilbitmap_ok = 0
|
_pilbitmap_ok = 0
|
||||||
return _pilbitmap_ok
|
return _pilbitmap_ok
|
||||||
|
|
||||||
|
def _get_image_from_kw(kw):
|
||||||
|
source = None
|
||||||
|
if "file" in kw:
|
||||||
|
source = kw.pop("file")
|
||||||
|
elif "data" in kw:
|
||||||
|
source = BytesIO(kw.pop("data"))
|
||||||
|
if source:
|
||||||
|
return Image.open(source)
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# PhotoImage
|
# PhotoImage
|
||||||
|
@ -80,13 +90,7 @@ class PhotoImage(object):
|
||||||
|
|
||||||
# Tk compatibility: file or data
|
# Tk compatibility: file or data
|
||||||
if image is None:
|
if image is None:
|
||||||
if "file" in kw:
|
image = _get_image_from_kw(kw)
|
||||||
image = Image.open(kw["file"])
|
|
||||||
del kw["file"]
|
|
||||||
elif "data" in kw:
|
|
||||||
from io import BytesIO
|
|
||||||
image = Image.open(BytesIO(kw["data"]))
|
|
||||||
del kw["data"]
|
|
||||||
|
|
||||||
if hasattr(image, "mode") and hasattr(image, "size"):
|
if hasattr(image, "mode") and hasattr(image, "size"):
|
||||||
# got an image instead of a mode
|
# got an image instead of a mode
|
||||||
|
@ -209,13 +213,7 @@ class BitmapImage(object):
|
||||||
|
|
||||||
# Tk compatibility: file or data
|
# Tk compatibility: file or data
|
||||||
if image is None:
|
if image is None:
|
||||||
if "file" in kw:
|
image = _get_image_from_kw(kw)
|
||||||
image = Image.open(kw["file"])
|
|
||||||
del kw["file"]
|
|
||||||
elif "data" in kw:
|
|
||||||
from io import BytesIO
|
|
||||||
image = Image.open(BytesIO(kw["data"]))
|
|
||||||
del kw["data"]
|
|
||||||
|
|
||||||
self.__mode = image.mode
|
self.__mode = image.mode
|
||||||
self.__size = image.size
|
self.__size = image.size
|
||||||
|
|
|
@ -1,15 +1,43 @@
|
||||||
from helper import unittest, PillowTestCase
|
from helper import unittest, PillowTestCase
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
try:
|
||||||
|
from PIL import ImageTk
|
||||||
|
dir(ImageTk)
|
||||||
|
except (OSError, ImportError) as v:
|
||||||
|
# Skipped via setUp()
|
||||||
|
pass
|
||||||
|
|
||||||
class TestImageTk(PillowTestCase):
|
class TestImageTk(PillowTestCase):
|
||||||
|
|
||||||
def test_import(self):
|
def setUp(self):
|
||||||
try:
|
try:
|
||||||
from PIL import ImageTk
|
from PIL import ImageTk
|
||||||
dir(ImageTk)
|
dir(ImageTk)
|
||||||
except (OSError, ImportError) as v:
|
except (OSError, ImportError) as v:
|
||||||
self.skipTest(v)
|
self.skipTest(v)
|
||||||
|
|
||||||
|
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)
|
||||||
|
with open(TEST_PNG, 'rb') as fp:
|
||||||
|
data = fp.read()
|
||||||
|
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.assertEqual(im, None)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user