Tutorial :Unable to open images with Python's Image.open()



Question:

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!


Solution:1

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?


Solution:2

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.


Solution:3

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'>  >>>   

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »