-3

I wrote this code:

import Image
im = raw_input("Insert Image file: ")
handle = Image.open(im)

print handle.size

to read an Image file and print its size, but when I run this code I get a traceback:

Traceback (most recent call last):
  File "image.py", line 1, in <module>
    import Image

P.S - I wrote the program in mac os x if it matters

  • What is `Image`? Have you defined it yourself? Is it a library? Are you sure you've installed it? – Ffisegydd Dec 16 '14 at 17:07
  • 1
    The last lines of the traceback are important. They tell us what type of exception was raised (it's probably an `ImportError` though). Could you include them please? –  Dec 16 '14 at 17:07
  • @iCodez oops, didn't notice I didn't add the last line: ImportError: No module named Image – user3027696 Dec 16 '14 at 17:39
  • @Ffisegydd I read here: http://effbot.org/imagingbook/introduction.htm that there's an existing class named Image, so I used it.. – user3027696 Dec 16 '14 at 17:42
  • 1
    That tutorial is 9 years old I think. I think what you're after is PIL. – Ffisegydd Dec 16 '14 at 17:44
  • @Ffisegydd I took a look at this question as well: http://stackoverflow.com/questions/3735553/how-do-i-read-an-image-file-using-python – user3027696 Dec 16 '14 at 17:48

2 Answers2

0

You most likely need to install PIL or Pillow. Here is the installation guide which can be summarized as:

  1. Install Brew:

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    
  2. Install some dependencies for Pillow:

    brew install libtiff libjpeg webp little-cms2
    
  3. Install Pillow:

    pip install Pillow
    

And change:

import Image

To:

from PIL import Image
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
0

If you name your file image.py, and then try to import Image there is a high risk that Python tries to import your own file and that all that ends in a stack overflow (the bad event, not the nice site).

NEVER give you files names that exists in Standard Python Library

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252