Discussion:
[Tkinter-discuss] displaying an image
Matthew Ngaha
2012-10-04 17:16:19 UTC
Permalink
im trying to display an image. Ive tried different code from several
tutorials but they seem outdated and nothing works. here's 1 line that
makes me give up.

my_image = Image.open("imagepath.jpg")

this line returns an error of Image doesnt have attribute open, yet
this is what my tutorial has shown me. can anyone please help?

any new tkinter tutorials around?
Michael O'Donnell
2012-10-04 17:29:50 UTC
Permalink
Hi Matthew,

Your first line should in fact be:

from PIL import Image

...then your line:

my_image = Image.open("imagepath.jpg")

Mick
Post by Matthew Ngaha
im trying to display an image. Ive tried different code from several
tutorials but they seem outdated and nothing works. here's 1 line that
makes me give up.
this line returns an error of Image doesnt have attribute open, yet
this is what my tutorial has shown me. can anyone please help?
any new tkinter tutorials around?
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
--
Not sent from my iPhone
Lynn Oliver
2012-10-04 18:23:54 UTC
Permalink
I know this isn't exactly what you asked, but I am displaying an image in a label in a --onefile distribution, and this code works both while debugging and while running the packaged application:

if getattr(sys, 'frozen', None):
basedir = sys._MEIPASS
else:
basedir = os.path.dirname(__file__)
self.photo = tk.PhotoImage(file=os.path.join(basedir, 'myImage.gif'))
self.myLabel = ttk.Label(self.myFrame, image=self.photo)
self.myLabel.grid()

Lynn
Post by Michael O'Donnell
Hi Matthew,
from PIL import Image
my_image = Image.open("imagepath.jpg")
Mick
Post by Matthew Ngaha
im trying to display an image. Ive tried different code from several
tutorials but they seem outdated and nothing works. here's 1 line that
makes me give up.
this line returns an error of Image doesnt have attribute open, yet
this is what my tutorial has shown me. can anyone please help?
any new tkinter tutorials around?
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
--
Not sent from my iPhone
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20121004/4804f629/attachment.html>
Matthew Ngaha
2012-10-04 18:59:31 UTC
Permalink
Post by Lynn Oliver
I know this isn't exactly what you asked, but I am displaying an image in a
label in a --onefile distribution, and this code works both while debugging
basedir = sys._MEIPASS
basedir = os.path.dirname(__file__)
self.photo = tk.PhotoImage(file=os.path.join(basedir,
'myImage.gif'))
self.myLabel = ttk.Label(self.myFrame, image=self.photo)
self.myLabel.grid()
Lynn
i am very new to programming and all that code is confusing me:( sorry:)
Post by Lynn Oliver
Hi Matthew,
from PIL import Image
my_image = Image.open("imagepath.jpg")
Mick
it gave me an ImportError saying no module named PIL
Michael O'Donnell
2012-10-04 19:11:26 UTC
Permalink
You need to install PIL to use the tutorial you are doing.

http://www.pythonware.com/products/pil/

PIL has some advantages to built in images within TKinter,
especially if you are wanting to manipulate images rather than
just display them

Mick
Post by Matthew Ngaha
Post by Michael O'Donnell
Hi Matthew,
from PIL import Image
my_image = Image.open("imagepath.jpg")
Mick
it gave me an ImportError saying no module named PIL
Matthew Ngaha
2012-10-04 21:02:07 UTC
Permalink
Well, you can download 2.7.3 (I think that's the current one) from
python.org so you aren't stuck on that front. I don't think PIL got
ported to Python 3.x -- it's more or less abandonware now.
so i can have 2 diff versions of Python on my computer? they won't
conflict with each other?
Cameron Laird
2012-10-04 21:06:15 UTC
Permalink
On Thu, Oct 04, 2012 at 10:02:07PM +0100, Matthew Ngaha wrote:
.
.
.
Post by Matthew Ngaha
Well, you can download 2.7.3 (I think that's the current one) from
python.org so you aren't stuck on that front. I don't think PIL got
ported to Python 3.x -- it's more or less abandonware now.
so i can have 2 diff versions of Python on my computer? they won't
conflict with each other?
.
.
.
It CERTAINLY is possible to have multiple Python
releases nicely installed on a single host, with-
out conflict. It's even common.

It's also possible to confuse things and create
considerable conflict.
Matthew Ngaha
2012-10-04 21:44:19 UTC
Permalink
Post by Cameron Laird
It CERTAINLY is possible to have multiple Python
releases nicely installed on a single host, with-
out conflict. It's even common.
It's also possible to confuse things and create
considerable conflict.
thanks for all the help guys. ive been trying to learn tkinter all day
with little luck so im too tired to continue. ill do everything you
guys told me tomorrow and hopefully if all goes well, i won't be
replying to this email, i'll be making a new one with brand new
problems:)

thanks again
Matthew Ngaha
2012-10-05 01:06:41 UTC
Permalink
Well actually, no. You can just install Python 2.7 and use PIL with
that. It might be better though to focus on things that do work in
Python 3 and find a tutorial that doesn't need PIL.
oh thats a shame. ive look for many tkinter tutorials and all seem to
only do it in Python 2. a book also would really help as they explain
in more detail, but im yet to find a book for tkinter. the only 1 i
saw was released in 2000:(
Michael O'Donnell
2012-10-05 06:30:37 UTC
Permalink
Reference guide at:

http://infohost.nmt.edu/tcc/help/pubs/tkinter.pdf

HTML vertsion of Fredrik Lundh's book on tkinter,
from 1999, but still valid for explaining how things work
(Tkinter has not changed much, although some things have
been added, especially TTK):

http://www.pythonware.com/library/tkinter/introduction/index.htm

A lot of people are still working withpython 2.7. Some modules have not
yet been ported to python3, and users are waiting for the port.
I recently ported my program to 3, and support for some things
is a bit buggy, although mostly there (e.g., issues in numpy, py2app).
And yes, PIL is not yet ported to python3.

Mick
Post by Matthew Ngaha
Well actually, no. You can just install Python 2.7 and use PIL with
that. It might be better though to focus on things that do work in
Python 3 and find a tutorial that doesn't need PIL.
oh thats a shame. ive look for many tkinter tutorials and all seem to
only do it in Python 2. a book also would really help as they explain
in more detail, but im yet to find a book for tkinter. the only 1 i
saw was released in 2000:(
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
--
Not sent from my iPhone
John W. Shipman
2012-10-05 06:44:56 UTC
Permalink
Post by Michael O'Donnell
http://infohost.nmt.edu/tcc/help/pubs/tkinter.pdf
Thanks for the plug, Mr. O'Donnell. I'm the author of that work.
A task on my agenda is to update it for the themed widgets, and
also somehow document the differences in Python 3.x. However,
it may be a few more months before I can dig down to it.

Comments, suggestions, and corrections always welcome.

Best regards,
John Shipman (john at nmt.edu), Applications Specialist
New Mexico Tech Computer Center, Speare 146, Socorro, NM 87801
(575) 835-5735, http://www.nmt.edu/~john
``Let's go outside and commiserate with nature.'' --Dave Farber
Matthew Ngaha
2012-10-05 09:22:43 UTC
Permalink
If you're using Windows, Christoph Gholke has an installer for an
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil
i have only Python 3.1. is 3.2/3.3 the only versions the installer
will work on.
He also has the ported source code available to build PIL on a
non-Windows platform. But be forewarned this is rarely a simple
process.
im on windows, but those steps are too complicated for me. i am only
slightly past the hello world stage:(
Matthew Ngaha
2012-10-05 18:10:35 UTC
Permalink
with no solution to my problem after 2 days of pulling my hair out:( i
realize that some libraries are best for the Python version they
support the most. Sadly most tkinter users use version 2 unlike me, so
im pretty much on my own as the solutions don't apply to me. Can
someone please recommend me a toolkit that is fully supported on
Python 3 and has a similar or easier learning curve to tkinter? thanks
for all the help and effort guys.
Bryan Oakley
2012-10-05 18:53:30 UTC
Permalink
Post by Matthew Ngaha
with no solution to my problem after 2 days of pulling my hair out:( i
realize that some libraries are best for the Python version they
support the most. Sadly most tkinter users use version 2 unlike me, so
im pretty much on my own as the solutions don't apply to me. Can
someone please recommend me a toolkit that is fully supported on
Python 3 and has a similar or easier learning curve to tkinter? thanks
for all the help and effort guys.
Matthew:

Sorry for your problems. Tkinter isn't usually so hard for people to
get started with.

Your original message showed you were trying to open a .jpg image. Do
you have the option of converting it to the GIF format? If so, you
don't need PIL at all since .GIF is directly supported by Tkinter.
For example, to display a GIF image you can do this:

class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.image = tk.PhotoImage(file=image_file)
label = tk.Label(self, image=self.image)
label.pack()


app = ExampleApp()
app.mainloop()

The above code works for me just fine with Python 3.2. All you need to
do is define 'image_file' to point to a .gif that you want to display.

Unfortunately Tkinter only directly supports .gif and the older
ppm/pgm formats. For any other format you'll need PIL, or some other
way to convert the image to a supported format.

Yeah, I know it's crazy that Tkinter only supports .GIF. For most
people who use Tkinter there are workarounds. Unfortunately, as a
newbie forced to use Python3, the workaround is not so evident.
Michael Lange
2012-10-05 19:20:17 UTC
Permalink
Hi Matthew,

On Fri, 5 Oct 2012 19:10:35 +0100
Post by Matthew Ngaha
with no solution to my problem after 2 days of pulling my hair out:( i
realize that some libraries are best for the Python version they
support the most. Sadly most tkinter users use version 2 unlike me, so
im pretty much on my own as the solutions don't apply to me. Can
someone please recommend me a toolkit that is fully supported on
Python 3 and has a similar or easier learning curve to tkinter? thanks
for all the help and effort guys.
maybe what confused you were some of the more "advanced" suggestions
about using the Python Imaging Library (PIL) or installing a second
Python version or discussing the handling of namespaces.

In fact, as far as I see, there is not too much difference in the
Tkinter usage between Python2.7 and Python3, so for the most part the
old Python2-based Tkinter tutorials should still serve you well. The most
evident difference is that since Python3 the tkinter module spells in
lower case.
Post by Matthew Ngaha
im trying to display an image. Ive tried different code from several
tutorials but they seem outdated and nothing works. here's 1 line that
makes me give up.
my_image = Image.open("imagepath.jpg")
this line returns an error of Image doesnt have attribute open, yet
this is what my tutorial has shown me. can anyone please help?
This line seems in fact to be taken from some tutorial describing the
usage of PIL. In plain Tkinter the simplest program displaying an image
might look like (Python3 version):


from tkinter import *
root = Tk()
img = PhotoImage(file='imagepath.gif')
l = Label(root, image=img)
l.pack()
root.mainloop()

The only difference to the "Python2-version" is the lower-case "Tkinter"
in the first line.
You see that in Tkinter you usually do not have to use the Image class at
all (although it exists) but create the image directly with the PhotoImage
class.
One (more or less serious) restriction of Tkinter's standard PhotoImage
class is however that you can only use GIF images, other formats like
JPEG or PNG are not supported yet. So unfortunately you will either have
to use an external tool to convert your jpegs into gif first or install a
third-party extension like PIL or TkImg* (the latter is easier to use if
you only need support for more file formats, because no commands are
changed or added, but just support for a lot of image file formats is
added silently in the background; I am not sure however if there is a
precompiled binary for windows systems is easily available) if you really
need these formats.

Did this help any?

Best regards

Michael

* http://sourceforge.net/projects/tkimg/


.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.

[War] is instinctive. But the instinct can be fought. We're human
beings with the blood of a million savage years on our hands! But we
can stop it. We can admit that we're killers ... but we're not going
to kill today. That's all it takes! Knowing that we're not going to
kill today!
-- Kirk, "A Taste of Armageddon", stardate 3193.0
Matthew Ngaha
2012-10-05 20:34:02 UTC
Permalink
Post by Michael Lange
. So unfortunately you will either have
to use an external tool to convert your jpegs into gif first or install a
third-party extension like PIL or TkImg* (the latter is easier to use if
you only need support for more file formats, because no commands are
changed or added, but just support for a lot of image file formats is
added silently in the background; I am not sure however if there is a
precompiled binary for windows systems is easily available) if you really
need these formats.
Did this help any?
* http://sourceforge.net/projects/tkimg/
thanks. Yes this was very helpful. im not sure when you say precompiled
binary for windows systems. i did download the Tkimg software from the link
you provided. is the next step as easy as extracting the zip file into my
Python folder or is it more involved?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20121005/99b7e42b/attachment.html>
Michael Lange
2012-10-06 09:57:18 UTC
Permalink
On Fri, 5 Oct 2012 21:34:02 +0100
Post by Matthew Ngaha
Post by Michael Lange
. So unfortunately you will either have
to use an external tool to convert your jpegs into gif first or
install a third-party extension like PIL or TkImg* (the latter is
easier to use if you only need support for more file formats, because
no commands are changed or added, but just support for a lot of image
file formats is added silently in the background; I am not sure
however if there is a precompiled binary for windows systems is
easily available) if you really need these formats.
Did this help any?
* http://sourceforge.net/projects/tkimg/
thanks. Yes this was very helpful. im not sure when you say precompiled
binary for windows systems. i did download the Tkimg software from the
link you provided. is the next step as easy as extracting the zip file
into my Python folder or is it more involved?
Yes, im afraid ;)
The first thing you should be aware is that TkImg does not have anything
to do with Python, it is purely an extension to Tcl/Tk.
So if you have the "binary" package the files must go into the Tcl folder
of your Python install, usually something like C:\Python31\tcl .

A binary tcl-extension Package usually contains one or more *.dll files
and a pkgIndex.tcl file; the latter is important for the Tcl Interpreter,
so it knows how to handle the dll(s).

Now I looked at the tkimg14.zip file myself, and actually there don't
seem to be any dlls, just the "source code" that must be
"compiled" ("translated" into machine code) first, which unfortunately
might not be too trivial for a beginner.

If you want to try TkImg anyway without having to compile it yourself,
you can look for example here:

http://www.posoft.de/html/extTkImg.html

They offer packages for "windows7" and for "windows" there, not sure if
these also work with xp or vista in case you still have one of these. I
think it might still be worth a try. Since Tk8.5 is there for years now,
I don't think the Tk version actualy in use should matter.

If you copy the contents of one of the .zip files offered there into your
PythonXY\tcl folder there might be a good chance that it "just works", I
never tested it myself. though.

Best regards

Michael



.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.

Our way is peace.
-- Septimus, the Son Worshiper, "Bread and Circuses",
stardate 4040.7.
Matthew Ngaha
2012-10-06 14:36:31 UTC
Permalink
Post by Michael Lange
If you want to try TkImg anyway without having to compile it yourself,
http://www.posoft.de/html/extTkImg.html
If you copy the contents of one of the .zip files offered there into your
PythonXY\tcl folder there might be a good chance that it "just works", I
never tested it myself. though.
nothing seems to work with TkImg but however the gif example you showed me
is working fine so i guess im happy with that as gimp successfully converts
my images into gifs. I will try sending an email to TkImg to see if they
can direct me in steps to installling the program.. ive really really
appreciated your help. Thanks for taking the time and effort to offer me
help.
Thanks to everyone else who commented also.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20121006/2a659088/attachment.html>
Michael Lange
2012-10-06 14:52:21 UTC
Permalink
Hi Matthew,

On Sat, 6 Oct 2012 15:36:31 +0100
Post by Matthew Ngaha
nothing seems to work with TkImg but however the gif example you
showed me is working fine so i guess im happy with that as gimp
successfully converts my images into gifs. I will try sending an email
to TkImg to see if they can direct me in steps to installling the
program.
sorry, what I completely forgot to mention about TkImg is that of course
after installing the dlls your program has to tell the Tcl/Tk Interpreter
to use it. The magic line for that in Tcl is:

package require Img

In Python / Tkinter this can be accomplished with a single line, too, as
in this snippet (note the third line):

from tkinter import *
root = Tk()
root.tk.call('package', 'require', 'Img')
img = PhotoImage(file='imagepath.jpg')
l = Label(root, image=img)
l.pack()
root.mainloop()

So in case you did not try this already, there may still be hope.

Best regards

Michael

.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.

Killing is stupid; useless!
-- McCoy, "A Private Little War", stardate 4211.8
Matthew Ngaha
2012-10-06 15:14:39 UTC
Permalink
Post by Michael Lange
sorry, what I completely forgot to mention about TkImg is that of course
after installing the dlls your program has to tell the Tcl/Tk Interpreter
package require Img
In Python / Tkinter this can be accomplished with a single line, too, as
from tkinter import *
root = Tk()
root.tk.call('package', 'require', 'Img')
img = PhotoImage(file='imagepath.jpg')
l = Label(root, image=img)
l.pack()
root.mainloop()
So in case you did not try this already, there may still be hope.
sorry that last email was sent directly by mistake. The above code has
fixed my issues and i am very happy right now:)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20121006/2c1d564b/attachment.html>
Lynn Oliver
2012-10-04 19:11:54 UTC
Permalink
I should be the one to apologize; I included code that is required for packages created by pyinstaller, which is a tool that will take a Python script and turn it into a self-contained executable. Let me try again:

import Tkinter as tk
import ttk

photo = tk.PhotoImage(file='myImageFile.gif'))
myLabel = ttk.Label(self.myFrame, image=self.photo)
myLabel.grid()

This is just the Tkinter code. I have "tk.PhotoImage()" instead of just "PhotoImage()" because of the way I imported Tkinter. Most examples will probably show this:

from Tkinter import *

so you would not need the "tk." prefix. I prefer to keep the namespaces separate.

I use "ttk.Label()" instead of "tk.Label()" because I like the newer ttk widgets better.
Post by Matthew Ngaha
Post by Lynn Oliver
I know this isn't exactly what you asked, but I am displaying an image in a
label in a --onefile distribution, and this code works both while debugging
basedir = sys._MEIPASS
basedir = os.path.dirname(__file__)
self.photo = tk.PhotoImage(file=os.path.join(basedir,
'myImage.gif'))
self.myLabel = ttk.Label(self.myFrame, image=self.photo)
self.myLabel.grid()
Lynn
i am very new to programming and all that code is confusing me:( sorry:)
Post by Lynn Oliver
Hi Matthew,
from PIL import Image
my_image = Image.open("imagepath.jpg")
Mick
it gave me an ImportError saying no module named PIL
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20121004/a72106ab/attachment-0001.html>
Matthew Ngaha
2012-10-04 19:29:31 UTC
Permalink
Post by Lynn Oliver
import Tkinter as tk
import ttk
i get an ImportError say no module named Tkinter:(
Lion Kimbro
2012-10-04 19:51:08 UTC
Permalink
Are you running a tutorial for Python 2 from Python 3?
Post by Matthew Ngaha
Post by Lynn Oliver
import Tkinter as tk
import ttk
i get an ImportError say no module named Tkinter:(
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20121004/c1b3e459/attachment.html>
Matthew Ngaha
2012-10-04 20:03:47 UTC
Permalink
Post by Lion Kimbro
Are you running a tutorial for Python 2 from Python 3?
sorry for the direct email. yes the tutorial uses Python 2, while i
only have Python 3
Lion Kimbro
2012-10-04 20:08:38 UTC
Permalink
The tkinter modules were renamed from 2-3.

I can't remember where the table is that shows the
renames/reorganization, but you need to know that the modules were renamed.

In Python 3, it's "import tkinter".
In Python 2, it's "import Tkinter".

Other modules within the tkinter hierarchy were renamed as well -- in
most cases, uppercase became lowercase. If memory serves right, there was
some reorganization of the hierarchy as well.
Post by Matthew Ngaha
Post by Lion Kimbro
Are you running a tutorial for Python 2 from Python 3?
sorry for the direct email. yes the tutorial uses Python 2, while i
only have Python 3
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss at python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20121004/d54eb70f/attachment.html>
Loading...