0

I'm trying to display an image in Tkinter using Pillow but I get a weird error: "Process finished with exit code -1073741819 (0xC0000005)" and the Tkinter window never appears.

Here's the code (simplified to the max):

from PIL import Image, ImageTk
from tkinter import Tk

t = Tk()
i = Image.open('data/pic.jpg') # small picture (29kb, 100x100px)
i_tk = ImageTk.PhotoImage(i) # The problem disappears when this line is commented out.
t.mainloop()

I'm using Python 3.5, Tcl/Tk 8.6, Pillow 3.0.0 on Windows 10 (all in 64bit)

That same script (replacing tkinter by Tkinter) runs perfectly on the same machine with Python 2.7.9, Tcl/Tk 8.5 and Pillow 2.9.0 (the Tk window appears and the exit code is 0 when I close the Tk window).

Any help would be much appreciated.

EDIT: Per user5510752's suggestion, I changed i_tk = ImageTk.PhotoImage(i) to i_tk = tkinter.PhotoImage(i). The problem has now shifted from where I was making the PhotoImage to where I'm inserting it in the canvas.

Here's the new code:

from PIL import Image
from tkinter import Tk, PhotoImage, Canvas

t = Tk()
c = Canvas(t, bg="blue")
c.grid(sticky="news")

c.i = Image.open("data/pic.jpg")
c.p = PhotoImage(c.i)
c.create_image(0, 0, image=c.p) # errors out here

t.mainloop()

This gives this error TypeError: __str__ returned non-string (type JpegImageFile).

Traceback (most recent call last):
  File "D:/Workspace/PythonProjects/Puzzle 3.5/main.py", line 29, in <module>
    c.create_image(0, 0, image=c.p)
  File "C:\Python 3.5\lib\tkinter\__init__.py", line 2328, in create_image
    return self._create('image', args, kw)
  File "C:\Python 3.5\lib\tkinter\__init__.py", line 2319, in _create
    *(args + self._options(cnf, kw))))
TypeError: __str__ returned non-string (type JpegImageFile)

I looked into a different signature for this function for Python 3, but I couldn't find anything. This line works on Python 2.7 though (with ImageTk.PhotoImage). What's weirder is that if I try to load c.i into the canvas instead of c.p, the code doesn't error out and I get an empty canvas.

[EDIT2]

As per R4PH43L's suggestion I tried :

from tkinter import Tk, PhotoImage, Canvas

t = Tk()
c = Canvas(t, bg="blue")
c.grid(sticky="news")

c.p=PhotoImage(file="data/pic.jpg")
c.create_image(0, 0, image=c.p) # errors out here

t.mainloop()

This gave a new error :

Traceback (most recent call last):
  File "D:/Workspace/PythonProjects/Puzzle 3.5/main.py", line 28, in <module>
    c.p=PhotoImage(file="data/pic.jpg")
  File "C:\Python 3.5\lib\tkinter\__init__.py", line 3393, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Python 3.5\lib\tkinter\__init__.py", line 3349, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "data/pic.jpg"

I've tried each time with different JPEGs to no avail. This time I also tried with a GIF and it worked (but I need to open JPEGs, so...). It's worth noting that libjpeg is installed on my machine and Pillow doesn't seem to have any trouble using it (except when I pass the pic to ImageTk, then we're back to my original error).

PS : If someone managed to show a jpg file in a tkinter canvas in python 3.5, please just post the version of Tcl/Tk, libjpeg and Pillow you're using. I suspect this may just be down to two modules being incompatible in my current Python 3 config.

takeshi2010
  • 155
  • 1
  • 12

3 Answers3

1

Your original code should work perfectly, but it seems there is something wrong with Pillow or Tk.

Anyway you cannot load jpg without ImageTk.PhotoImage() so there is no way to work it out with Pillow and Tk until someone will understand what is causing crash.

And i have the same issue with pillow 3.0 wheel, but it wasn't really critical to me...

JFYI callstack: https://gist.github.com/DoumanAsh/6dba8411a109fbc68197

P.s. i have the same setup as you're. And i suspect that python 3.5 on Win platform should use only Tk 8.6 because windows bundle was updated with Tcl 8.6.3 and that introduced Tcl's support for Win10

UPD:

The issue is fixed https://github.com/python-pillow/Pillow/pull/1553

I assume 3.0.1 version will contain this fix

Douman
  • 66
  • 7
  • Sorry I cannot upvote your answer yet, but I would have if I could. Thank you for the constructive answer :-). Now I know it's not something to do with my install. I ended up reverting to Python 2.7.9 for my project and apart for some nifty tricks of Python 3 that I couldn't use, everything worked just fine. I wish they could fix it though. – takeshi2010 Nov 22 '15 at 00:19
  • (I accepted it, though, as I don't see a better answer yet) – takeshi2010 Nov 22 '15 at 00:25
  • Your question though reminded me about this issue and raised it to pillow guys. Maybe they will have some suggestions about it: https://github.com/python-pillow/Pillow/issues/1549 – Douman Nov 22 '15 at 06:38
  • I just had this same problem (Windows 10, Python 3.5). I upgraded to Pillow 3.2.0 and it's all working now. – MRAB May 22 '16 at 19:33
0

Most of the tkinter functions have changed for python 3, your variable, i_tk, must be like this:

i_tk = tkinter.PhotoImage(i)
  • Thanks for the suggestion. I've updated the question as the problem seems to have been pushed further down the line. – takeshi2010 Oct 31 '15 at 23:12
0

Have a look at this question.

Try creating your image as c.p=tkinter.PhotoImage(file="data/pic.jpg").

as you can see in your stacktrace, the error occurs at place off the mapping of arguments and keyword arguments (*args, **kwargs). as you do just pass the image without keyword, it tries to parse your Image.open([...]) as a positional argument. This fails as positional arguments are read as strings over here. This is what your stacktrace tells us.

One look at the documentation:

The PhotoImage class can read GIF and PGM/PPM images from files

Community
  • 1
  • 1
R4PH43L
  • 2,122
  • 3
  • 18
  • 30
  • Thanks for the link and the suggestion. I read them thoroughly and made some tests based on them. It didn't work. I've edited the question with the new stack trace. Thanks for your help. – takeshi2010 Nov 02 '15 at 22:10
  • wait a sec, i'll install py3 and test it. you already checked the jpeg file, didn't you? – R4PH43L Nov 02 '15 at 22:13
  • Thanks. I checked the jpg file and tested with several others from different sources. Nothing worked for jpg files. GIF worked though. – takeshi2010 Nov 02 '15 at 22:18
  • Yes I knew about that part of the doc, but what they advice for JPEG files is what I was trying to do in the first place and it doesn't work (though the error I get in that case is quite weird: no stack trace, just a dirty exit code). – takeshi2010 Nov 02 '15 at 22:26
  • You need to use [PIL][1] or [pillow][2] over here. Sorry, seems i did not read your question carefully enough. [1]: https://pypi.python.org/pypi/PIL [2]: https://pypi.python.org/pypi/Pillow – R4PH43L Nov 02 '15 at 22:28
  • "I'm using Python 3.5, Tcl/Tk 8.6, Pillow 3.0.0 on Windows 10 (all in 64bit)" ;-) – takeshi2010 Nov 02 '15 at 22:29
  • but you are using `tkinter.PhotoImage`, not PIL PhotoImage Class – R4PH43L Nov 02 '15 at 22:30
  • My first attempt (documented at the very top of my question) shows how it doesn't work with Pillow's `ImageTk.PhotoImage`. It's only in the second and third attempt, based on suggestion made by you and user5510752 that I moved to `tkinter.PhotoImage`. Could you try to make the code work with your Python 3.5 and tell me which version of Pillow and Tcl/Tk you're using ? Thanks :) – takeshi2010 Nov 02 '15 at 22:36