From ca3f6a25f40f6c51d69c71f1206e337684a36ef8 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Tue, 17 Jan 2017 06:20:39 -0800 Subject: [PATCH 1/4] Applied patch: https://github.com/matplotlib/matplotlib/commit/a91559b82cf23ee407cd57580653015fc7dc35f0 to fix issue #1902 --- _imagingtk.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/_imagingtk.c b/_imagingtk.c index 87de36a04..d0295f317 100644 --- a/_imagingtk.c +++ b/_imagingtk.c @@ -36,18 +36,18 @@ _tkinit(PyObject* self, PyObject* args) { Tcl_Interp* interp; - Py_ssize_t arg; + PyObject* arg; int is_interp; - if (!PyArg_ParseTuple(args, "ni", &arg, &is_interp)) + if (!PyArg_ParseTuple(args, "Oi", &arg, &is_interp)) return NULL; if (is_interp) - interp = (Tcl_Interp*) arg; + interp = (Tcl_Interp*)PyLong_AsVoidPtr(arg); else { TkappObject* app; /* Do it the hard way. This will break if the TkappObject layout changes */ - app = (TkappObject*) arg; + app = (TkappObject*)PyLong_AsVoidPtr(arg); interp = app->interp; } From e66271d46477cedca2ffb18a1206e3436f7710d8 Mon Sep 17 00:00:00 2001 From: Eric Soroos Date: Tue, 24 Jan 2017 05:43:58 -0800 Subject: [PATCH 2/4] added tests for functionality in ImageTk --- Tests/test_imagetk.py | 63 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/Tests/test_imagetk.py b/Tests/test_imagetk.py index f56333a59..1d5a281a6 100644 --- a/Tests/test_imagetk.py +++ b/Tests/test_imagetk.py @@ -1,22 +1,29 @@ -from helper import unittest, PillowTestCase +from helper import unittest, PillowTestCase, hopper from PIL import Image + try: from PIL import ImageTk + import Tkinter as tk dir(ImageTk) + HAS_TK = True except (OSError, ImportError) as v: # Skipped via setUp() - pass - + HAS_TK = False + +TK_MODES = ('1', 'L', 'P', 'RGB', 'RGBA') class TestImageTk(PillowTestCase): def setUp(self): + if not HAS_TK: + self.skipTest("Tk not installed") try: - from PIL import ImageTk - dir(ImageTk) - except (OSError, ImportError) as v: - self.skipTest(v) + # setup tk + app = tk.Frame() + #root = tk.Tk() + except (tk.TclError) as v: + self.skipTest("TCL Error: %s" % v) def test_kw(self): TEST_JPG = "Tests/images/hopper.jpg" @@ -40,5 +47,47 @@ class TestImageTk(PillowTestCase): self.assertEqual(im, None) + def test_photoimage(self): + for mode in TK_MODES: + # test as image: + im = hopper(mode) + + # this should not crash + im_tk = ImageTk.PhotoImage(im) + + self.assertEqual(im_tk.width(), im.width) + self.assertEqual(im_tk.height(), im.height) + + # _tkinter.TclError: this function is not yet supported + #reloaded = ImageTk.getimage(im_tk) + #self.assert_image_equal(reloaded, im) + + + + def test_photoimage_blank(self): + # test a image using mode/size: + for mode in TK_MODES: + im_tk = ImageTk.PhotoImage(mode, (100,100)) + + self.assertEqual(im_tk.width(), 100) + self.assertEqual(im_tk.height(), 100) + + #reloaded = ImageTk.getimage(im_tk) + #self.assert_image_equal(reloaded, im) + + def test_bitmapimage(self): + im = hopper('1') + + # this should not crash + im_tk = ImageTk.BitmapImage(im) + + self.assertEqual(im_tk.width(), im.width) + self.assertEqual(im_tk.height(), im.height) + + #reloaded = ImageTk.getimage(im_tk) + #self.assert_image_equal(reloaded, im) + + + if __name__ == '__main__': unittest.main() From a69c37738ab5bf370e007f762272710a86a9bd5a Mon Sep 17 00:00:00 2001 From: wiredfool Date: Thu, 26 Jan 2017 05:30:49 -0800 Subject: [PATCH 3/4] Look in a different location for the tk intepreter on pypy fixes #2376 --- PIL/ImageTk.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/PIL/ImageTk.py b/PIL/ImageTk.py index 25c4534ce..d0d6fa8fd 100644 --- a/PIL/ImageTk.py +++ b/PIL/ImageTk.py @@ -32,6 +32,13 @@ except ImportError: tkinter = Tkinter del Tkinter +# required for pypy, which always has cffi installed +try: + from cffi import FFI + ffi = FFI() +except ImportError: + pass + from . import Image from io import BytesIO @@ -184,7 +191,13 @@ class PhotoImage(object): try: from . import _imagingtk try: - _imagingtk.tkinit(tk.interpaddr(), 1) + if hasattr(tk, 'interp'): + # Pypy is using a ffi cdata element + # (Pdb) self.tk.interp + # + _imagingtk.tkinit(int(ffi.cast("uintptr_t", tk.interp)), 1) + else: + _imagingtk.tkinit(tk.interpaddr(), 1) except AttributeError: _imagingtk.tkinit(id(tk), 0) tk.call("PyImagingPhoto", self.__photo, block.id) From a71ba1a1c63678c701524203788bf2d3c733d1e5 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Thu, 26 Jan 2017 05:55:18 -0800 Subject: [PATCH 4/4] comments --- PIL/ImageTk.py | 2 ++ Tk/tkImaging.c | 7 ++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/PIL/ImageTk.py b/PIL/ImageTk.py index d0d6fa8fd..8c6d1a9c2 100644 --- a/PIL/ImageTk.py +++ b/PIL/ImageTk.py @@ -277,6 +277,8 @@ class BitmapImage(object): def getimage(photo): + """ This function is unimplemented """ + """Copies the contents of a PhotoImage to a PIL image memory.""" photo.tk.call("PyImagingPhotoGet", photo) diff --git a/Tk/tkImaging.c b/Tk/tkImaging.c index 6c612cfe9..f448be166 100644 --- a/Tk/tkImaging.c +++ b/Tk/tkImaging.c @@ -24,10 +24,7 @@ * This registers a Tcl command called "PyImagingPhoto", which is used * to communicate between PIL and Tk's PhotoImage handler. * - * Compile and link tkImaging.c with tkappinit.c and _tkinter (see the - * Setup file for details on how to use tkappinit.c). Note that - * _tkinter.c must be compiled with WITH_APPINIT. - * + * History: * 1995-09-12 fl Created * 1996-04-08 fl Ready for release @@ -169,7 +166,7 @@ PyImagingPhotoPut(ClientData clientdata, Tcl_Interp* interp, return TCL_OK; } - +/* Warning -- this does not work at all */ static int PyImagingPhotoGet(ClientData clientdata, Tcl_Interp* interp, int argc, const char **argv)