| | |
Are .JPGs possible in Tkinter?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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).
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).
•
•
Join Date: Dec 2006
Posts: 1,028
Reputation:
Solved Threads: 289
AFAIK you use the Python Imaging Library http://www.pythonware.com/library/index.htm Perhaps someone else will know more.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Exactly so. Install PIL from the link above, and then
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:
Because there is no permanent reference to the return value from PhotoImage, you will get a blank image.
Jeff
Python Syntax (Toggle Plain Text)
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:
Python Syntax (Toggle Plain Text)
... 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
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.
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.
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...
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:
python Syntax (Toggle Plain Text)
# 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()
No one died when Clinton lied.
![]() |
Other Threads in the Python Forum
- Previous Thread: What's a good python debugger?
- Next Thread: text adventure game problem!
| Thread Tools | Search this Thread |
address anydbm app beginner changecolor cipher class conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float format ftp function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse mysqldb number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile shebang slicenotation split string strings table terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web windows wx.wizard wxpython xlwt






