views:

142

answers:

3

My code reads:

import Image
def generateThumbnail(self, width, height):
    """
    Generates thumbnails for an image
    """
    im = Image.open(self._file)

When I call this function, I get an error:

⇝ AttributeError: type object 'Image' has no attribute 'open'

However in the console:

import Image
im = Image.open('test.jpg')

I have no problem. Any ideas? Thanks!

+1  A: 

Does your actual code have the incorrect statements:

from Image import Image

or

from Image import *

The Image module contains an Image class, but they are of course different (the module has an open method). If you use either of these two forms, Image will incorrectly refer to the class.

EDIT: Another possiblity is that you re defining a conflicting Image name yourself. Do you have your own Image class? If so, rename it.

Matthew Flaschen
Hi -- I tried both of these without success.
ensnare
Those lines were possible causes of the error, not recommendations. If you weren't using code like that before, Chris is probably right. Image is getting assigned the wrong value somewhere else (maybe you have your own Image class?)
Matthew Flaschen
+2  A: 

It's odd that you're getting an exception about Image being a type object, not a module. Is 'Image' being assigned to elsewhere in your code?

Chris AtLee
This was a stupid question, sorry. The name of my class was Image so there was a conflict. Thanks for helping!
ensnare
A: 

This is consistent with you having created a (new-style) class called Image, or imported it from somewhere else (perhaps inadvertently, from a "*" import), at some point after importing "Image":

>>> import Image
>>> Image.open
<function open at 0x99e3b0>
>>> class Image(object): pass
... 
>>> Image.open
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Image' has no attribute 'open'
>>> 

Look for this. You can check with "print Image", which should give you something like:

>>> print Image
<class 'foo.Image'>
>>> 
rbp