1

I am new to Stackoverflow and still learning python but I am attempting to begin a project of mine. I plan on creating a filter for images using python. The first step I would like to learn is how to open up an image in python. This is what I have so far:

from PIL import Image
im = Image.open('Jordan.jpg')
im.show()

Am I on the right track? Iam using Python 2.7

MackM
  • 2,906
  • 5
  • 31
  • 45
JosephM
  • 31
  • 1
  • 9
  • 1
    When you run the code what happens? any error? – SSC Jan 28 '15 at 05:53
  • Didn't this work? I think any tutorial on PIL will tell you how to open an image. I'd also recommend checking out scikit-image if PIL doesn't fit your needs. – Adam Hughes Jan 28 '15 at 05:53
  • 5
    use [Pillow](https://pypi.python.org/pypi/Pillow/) instead of PIL. PIL is deprecated. – levi Jan 28 '15 at 05:55
  • @SSC I get an error saying "Windows Photo Viewer cant open this picture because either the picture is deleted or its in a location that isnt available". I have the photo inside the same folder as in the .py file – JosephM Jan 28 '15 at 06:02
  • Works for me. Are you sure you've got the filename correct and the .jpg is in the working directory? Maybe give to full path of the jpg just to make sure that's not the problem... – hft Jan 28 '15 at 06:08
  • @hft how can I make sure the .jpg is in the working directory? – JosephM Jan 28 '15 at 06:16
  • Are you invoking your script from its own directory or some other? – Paul Rooney Jan 28 '15 at 06:26
  • @JosephM for setting the working directory check here http://stackoverflow.com/questions/1810743/how-to-set-the-current-working-directory-in-python – aberna Jan 28 '15 at 06:28
  • http://pillow.readthedocs.org/en/latest/handbook/tutorial.html – lxx Jan 28 '15 at 07:12
  • Try with the full path to the image to rule out if the path is wrong. Alternately try to open the image as a file and see if that works. – Paul Rooney Jan 28 '15 at 07:23
  • Hello Joseph, welcome to Stack Overflow. In its current state, I think your question is too broad for the SO format. Please share the errors you've encountered or the specific question you have so that we can help you better. – MackM Jan 28 '15 at 17:02
  • @JosephM, you can print the value of os.getcwd() to check what directory you are in. To do this you need to import the os module first. – hft Jan 28 '15 at 19:07

1 Answers1

1

the most natural python object that corresponds to an image is a multi-dimensional array; that's the most natural representation and it's also the best data structure to manipulate an image, as you intend to do by creating a filter.

Python has an array module, however, for creating and manipulating multi-dimensional arrays (eg, creating a filter), i highly recommend installing two python libraries NumPy and SciPy either from the links supplied here or pip installing them:

pip install numpy
pip install scipy


>>> from scipy import misc as MSC
>>> imfile = "~/path/to/an/image.jpg"

use the imread function in scipy's miscellaneous module, which in fact just calls PIL under the hood; this function will read in jpeg as well as png images

>>> img = MSC.imread(imfile)

verify that it is a NumPy array

>>> isinstance(img, NP.ndarray)
True 

a color image has three axes: X (width) Y (height), and Z (the three elements that comprise an RGB tuple;

first two axes are the pixel width and height of the image, the last dimension should be '3' (or '4' for an 'png' image which has an alpha channel)

>>> img.shape
(200, 200, 3)

selecting one pixel roughly from the image center (a single RGB 3-tuple)

>>> img[100,100,:]
array([83, 26, 17], dtype=uint8)

if you call Matplotlib's imshow method and pass in this NumPy array, it will render the image

doug
  • 69,080
  • 24
  • 165
  • 199