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.