PIL Open Problem

R

Richard Holmes

I'm trying to create an image for use in Tkinter. If I understand the
PIL documentation correctly, I first need to import Image, then
create an instance of the Image class and call 'open' (this according
to the documentation). When I try to do this using the model in the
documentation, I get:

Traceback (most recent call last):
File "todo.py", line 202, in <module>
im = Image.open(cwd + r'\delete.GIF', 'r')
AttributeError: class Image has no attribute 'open'

where 'cwd' is the current working directory obtained using the os
module. When I import Image into IDLE and enter help(Image.open), IDLE
is quite happy to give me the info about the function.

Can someone point out to me the folly of my ways?

TIA!

Dick Holmes
 
C

Corey Richardson

I'm trying to create an image for use in Tkinter. If I understand the
PIL documentation correctly, I first need to import Image, then
create an instance of the Image class and call 'open'

Don't do that. This is wrong:

import Image
im = Image.Image()
im = im.open(foo)

This is good:

import Image
im = Image.open(foo)
 
R

Richard Holmes

Don't do that. This is wrong:

import Image
im = Image.Image()
im = im.open(foo)

This is good:

import Image
im = Image.open(foo)
Uh, thanks, Corey, but that's what I'm doing. See Traceback:

Traceback (most recent call last):
File "todo.py", line 202, in <module>
im = Image.open(cwd + r'\delete.GIF', 'r')
AttributeError: class Image has no attribute 'open'
 
R

Richard Holmes

Please show the code you used that generated this traceback.


The line of code shown in the traceback is calling the ‘open’ method on
the ‘Image’ class – which has no such method, as the error tells you.


It will be much easier when we see some code. Post a minimal, working
example that still shows the behaviour you want explained.

Thanks, Ben. It turns out that I imported both Image and Tkinter and
Tkinter has an Image class that masked the Image class in the Image
module. I solved the problem by moving the Image code to a separate
module
 
R

rantingrick

Thanks, Ben. It turns out that I imported both Image and Tkinter and
Tkinter has an Image class that masked the Image class in the Image
module. I solved the problem by moving the Image code to a separate
module

Yes an another great example of why "from Tkinter import *" is a very
*very* bad idea. Use "import Tkinter as tk" to solve the dilemma. No
need to export code to another module. Next time you have an object
that should have an attribute but does not, print the repr() of the
object to find out what you are working with before clawing your
eyeballs out in frustration :).

<class Tkinter.Image at 0x027429C0>


use the repr() function in a script ... print repr(Image)
 
R

Richard Holmes

This is a classic problem known as “namespace clobbering”.

It is best to *avoid* the recommendations made in many libraries of
‘from foo import *’, because that will clobber any names in your
namespace that happen to match names in the ‘foo’ module.

Rather, import Tkinter and PIL as distinct namespaces::


and then you know that none of the names from those modules will clobber
existing ones.

Thanks. Message understood.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top