I've been trying to get some .JPGs to show up in Tkinter but have had no luck so far. Up until now I was just using PhotoImage, which takes .GIFs, but it would be nice if I could expand and use other formats. Any ideas?

Also, I know that wxPython is better thank Tkinter and can probably display .JPGs, but I've only just dabbled in it so far, and it looks much more complex (and Tkinter seems to cover 95% of my needs).

Exactly so. Install PIL from the link above, and then

from PIL import Image, ImageTk

my_im = Image.open("myjpg.jpg")
real_im = ImageTk.PhotoImage(my_im)

and now you can use real_im in your Tkinter code.

WARNING!!!

There is a bug in Tkinter such that if you do not keep a hard reference to your image, it will mysteriously get garbage-collected and disappear. This means that the following code does NOT work:

...
my_im = Image.open("myjpg.jpg")
b = Button(image=ImageTk.PhotoImage(my_im), etc.)

Because there is no permanent reference to the return value from PhotoImage, you will get a blank image.

Jeff

Comments
Thanks for pointing out the Tkinter bug

Great, thanks Jeff! I will give PIL a try.

Hey, so this is what I get when I try PIL:

AttributeError: 'module' object has no attribute 'Image'

Sigh. So it looks like my install didn't go well (which I knew since it gave me an error -- could not call command gcc). Of course, I followed the instructions for installation to the letter. Why does installing python libraries always have to be such a big, painful mystery? I always end up half relying on prayer.

could not call command gcc

Which Linux distro are you using. PIL may be in the repositories for your distro and you can install the normal way. To compile from source you will have to install the libraries used to compile. Again that depends on the distro you are using.

I am using Mac OSX actually. I'm pretty sure I downloaded the right version -- it was the only one that said "all platforms" instead of "Windows only."

In the README it didn't give any options other than installing at the command line via "python setup.py install." I'm at a loss for what else to try...

Be aware that there is a namspace conflict with class Image:

# load and display an image with Tkinter
# Tkinter only reads gif and ppm images
# use Python Image Library (PIL) for other image formats
# give Tkinter a namespace to avoid conflicts with PIL
# (they both have class Image) PIL is free from:
# http://www.pythonware.com/products/pil/index.htm

import Tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()
cv1 = tk.Canvas(root, width=500, height=500)
cv1.pack(fill='both', expand='yes')

# open a jpeg file into an image object
# image should be in the source folder or give full path name
image1 = Image.open("flowers.jpg")
# convert image object to Tkinter PhotoImage object
tkimage1 = ImageTk.PhotoImage(image1)

# tk.NW anchors upper left corner of image
# at canvas coordinates x=10, y=20
cv1.create_image(10, 20, image=tkimage1, anchor=tk.NW)

root.mainloop()

Thanks sneekula. I tried the code, but I still get an error:

ImportError: The _imaging C module is not installed

:(

This must be specific to the Mac OSX. I wonder if PIL has a users forum, or a place you can ask.

You don't have a compiler installed so you can't compile the source. There has to be pre-compiled binaries for the Mac. Search for something like "py-pil" in whatever Mac's use to install software. If you don't find anything then you will have to install XCode developer tools, which contain the compiler, etc (I think). This question is better asked on the Mac forums since it deals with installing software and not Python.

This question has already been answered. Start a new discussion instead.