943,870 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1734
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 4th, 2008
0

Are .JPGs possible in Tkinter?

Expand Post »
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).
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Apr 4th, 2008
0

Re: Are .JPGs possible in Tkinter?

AFAIK you use the Python Imaging Library http://www.pythonware.com/library/index.htm Perhaps someone else will know more.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Apr 4th, 2008
2

Re: Are .JPGs possible in Tkinter?

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

Python Syntax (Toggle Plain Text)
  1. from PIL import Image, ImageTk
  2.  
  3. my_im = Image.open("myjpg.jpg")
  4. 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:

Python Syntax (Toggle Plain Text)
  1. ...
  2. my_im = Image.open("myjpg.jpg")
  3. 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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Apr 5th, 2008
0

Re: Are .JPGs possible in Tkinter?

Great, thanks Jeff! I will give PIL a try.
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Apr 6th, 2008
0

Re: Are .JPGs possible in Tkinter?

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.
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Apr 6th, 2008
0

Re: Are .JPGs possible in Tkinter?

Quote ...
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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Apr 6th, 2008
0

Re: Are .JPGs possible in Tkinter?

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...
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Apr 6th, 2008
0

Re: Are .JPGs possible in Tkinter?

Be aware that there is a namspace conflict with class Image:
python Syntax (Toggle Plain Text)
  1. # load and display an image with Tkinter
  2. # Tkinter only reads gif and ppm images
  3. # use Python Image Library (PIL) for other image formats
  4. # give Tkinter a namespace to avoid conflicts with PIL
  5. # (they both have class Image) PIL is free from:
  6. # http://www.pythonware.com/products/pil/index.htm
  7.  
  8. import Tkinter as tk
  9. from PIL import Image, ImageTk
  10.  
  11. root = tk.Tk()
  12. cv1 = tk.Canvas(root, width=500, height=500)
  13. cv1.pack(fill='both', expand='yes')
  14.  
  15. # open a jpeg file into an image object
  16. # image should be in the source folder or give full path name
  17. image1 = Image.open("flowers.jpg")
  18. # convert image object to Tkinter PhotoImage object
  19. tkimage1 = ImageTk.PhotoImage(image1)
  20.  
  21. # tk.NW anchors upper left corner of image
  22. # at canvas coordinates x=10, y=20
  23. cv1.create_image(10, 20, image=tkimage1, anchor=tk.NW)
  24.  
  25. root.mainloop()
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Apr 6th, 2008
0

Re: Are .JPGs possible in Tkinter?

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

ImportError: The _imaging C module is not installed

aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Apr 6th, 2008
0

Re: Are .JPGs possible in Tkinter?

This must be specific to the Mac OSX. I wonder if PIL has a users forum, or a place you can ask.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: What's a good python debugger?
Next Thread in Python Forum Timeline: text adventure game problem!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC