How to open PIL image in Tkinter on canvas

I cannot get PIL Image to work on canvas. The code:

from Tkinter import* import Image, ImageTk root = Tk() root.geometry('1000x1000') canvas = Canvas(root,width=999,height=999) canvas.pack() image = ImageTk.PhotoImage("ball.gif") imagesprite = canvas.create_image(400,400,image=image) root.mainloop() 

Mistake:

 Traceback (most recent call last): File "C:/Users/Mark Malkin/Desktop/3d Graphics Testing/afdds.py", line 7, in <module> image = ImageTk.PhotoImage("ball.gif") File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 109, in __init__ mode = Image.getmodebase(mode) File "C:\Python27\lib\site-packages\PIL\Image.py", line 245, in getmodebase return ImageMode.getmode(mode).basemode File "C:\Python27\lib\site-packages\PIL\ImageMode.py", line 50, in getmode return _modes[mode] KeyError: 'ball.gif' 

I need to use PIL images, not PhotoImages, because I want to resize my images. Please do not suggest switching to Pygame, because I want to use Tkinter.

+8
python tkinter python-imaging-library tkinter-canvas
source share
4 answers

First try creating a PIL image and then use this to create a PhotoImage.

 from Tkinter import * import Image, ImageTk root = Tk() root.geometry('1000x1000') canvas = Canvas(root,width=999,height=999) canvas.pack() pilImage = Image.open("ball.gif") image = ImageTk.PhotoImage(pilImage) imagesprite = canvas.create_image(400,400,image=image) root.mainloop() 
+8
source share

(An old question, but the answers are still half full.)

Read the docs:

 class PIL.ImageTk.PhotoImage(image=None, size=None, **kw) 
  • image - PIL image or mode line. [...]
  • file is the name of the file to download the image (using Image.open(file) ).

So in your example use

 image = ImageTk.PhotoImage(file="ball.gif") 

or explicitly

 image = ImageTk.PhotoImage(Image("ball.gif")) 

(And remember, how you did it right: keep a reference to the image object in your Python program, otherwise it will be garbage collection before you see it.)

+3
source share

You can import multiple image formats and resize using this code. "basewidth" sets the width of your image.

 from Tkinter import * import PIL from PIL import ImageTk, Image root=Tk() image = Image.open("/path/to/your/image.jpg") canvas=Canvas(root, height=200, width=200) basewidth = 150 wpercent = (basewidth / float(image.size[0])) hsize = int((float(image.size[1]) * float(wpercent))) image = image.resize((basewidth, hsize), PIL.Image.ANTIALIAS) photo = ImageTk.PhotoImage(image) item4 = canvas.create_image(100, 80, image=photo) canvas.pack(side = TOP, expand=True, fill=BOTH) root.mainloop() 
+2
source share

I banged my head against the wall for a long time on this issue, until I discovered the following:

http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm

Apparently, the Python garbage collector can destroy the ImageTk object. I suggest that applications that use many widgets (such as mine) are more susceptible to this behavior.

0
source share

All Articles