Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PIL NameError global name Image is not defined

I installed PIL 1.1.7 using the executable for Python 2.7. But when I run this code :

import requests
from PIL import *

def main() :
    target = "http://target/image.php" #returns binary data for a PNG.
    cookies = {"mycookie1", "mycookie2"}
    r = requests.get(target, cookies=cookies)
    im = Image.open(r.content) #r.content contains binary data for the PNG.
    print im

if __name__ == "__main__" :
    main()

It gives the error :

Traceback (most recent call last):
File "D:\Programming\Python\code\eg_cc1.py", line 17, in <module>
  main()
File "D:\Programming\Python\code\eg_cc1.py", line 13, in main
  im = Image.open(r.content)
NameError: global name 'Image' is not defined

I installed PIL in Lib\site-packages.

like image 796
Mayank Kumar Avatar asked Dec 07 '13 16:12

Mayank Kumar


People also ask

How do I import a picture into PIL?

To load the image, we simply import the image module from the pillow and call the Image. open(), passing the image filename. Instead of calling the Pillow module, we will call the PIL module as to make it backward compatible with an older module called Python Imaging Library (PIL).


1 Answers

You need to explicitly import the Image module:

>>> from PIL import *
>>> Image
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Image' is not defined
>>> import PIL.Image
>>> Image
<module 'Image' from 'c:\Python27\lib\site-packages\PIL\Image.pyc'>
>>>

Or just

>>> import Image
like image 97
Peter Gibson Avatar answered Oct 13 '22 10:10

Peter Gibson