Are .JPGs possible in Tkinter?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Are .JPGs possible in Tkinter?

 
0
  #1
Apr 4th, 2008
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).
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,028
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 289
woooee woooee is offline Offline
Veteran Poster

Re: Are .JPGs possible in Tkinter?

 
0
  #2
Apr 4th, 2008
AFAIK you use the Python Imaging Library http://www.pythonware.com/library/index.htm Perhaps someone else will know more.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Are .JPGs possible in Tkinter?

 
2
  #3
Apr 4th, 2008
Exactly so. Install PIL from the link above, and then

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Re: Are .JPGs possible in Tkinter?

 
0
  #4
Apr 5th, 2008
Great, thanks Jeff! I will give PIL a try.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Re: Are .JPGs possible in Tkinter?

 
0
  #5
Apr 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,028
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 289
woooee woooee is offline Offline
Veteran Poster

Re: Are .JPGs possible in Tkinter?

 
0
  #6
Apr 6th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Re: Are .JPGs possible in Tkinter?

 
0
  #7
Apr 6th, 2008
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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Are .JPGs possible in Tkinter?

 
0
  #8
Apr 6th, 2008
Be aware that there is a namspace conflict with class Image:
  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()
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Re: Are .JPGs possible in Tkinter?

 
0
  #9
Apr 6th, 2008
Thanks sneekula. I tried the code, but I still get an error:

ImportError: The _imaging C module is not installed

Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Are .JPGs possible in Tkinter?

 
0
  #10
Apr 6th, 2008
This must be specific to the Mac OSX. I wonder if PIL has a users forum, or a place you can ask.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC