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

12
  • 1
    When you run the code what happens? any error? Commented Jan 28, 2015 at 5: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. Commented Jan 28, 2015 at 5:53
  • 5
    use Pillow instead of PIL. PIL is deprecated. Commented Jan 28, 2015 at 5: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 Commented Jan 28, 2015 at 6: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... Commented Jan 28, 2015 at 6:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.