Ask Your Question

How can I access an Image Pixel by Pixel?

asked 2012-01-23 15:52:29 -0500

anonymous user


If I want to perform an operation that searches an image to find, for example, the left most pixel of a certain color, will I have to just loop through all of the pixels? I know that sometimes numpy arrays can be used to avoid looping through all of the pixels but I don't know if that applies in most cases or not. Could someone let me know if I can perform a task like the one mentioned above without using nested loops? Or is that generally how a task like that is completed?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2012-01-23 16:57:00 -0500

kscottz gravatar image

In general, one should avoid accessing images on a pixel by pixel level unless it is absolutely necessary. Try performing a threshold on your image and then filtering the blobs.

edit flag offensive delete link more

answered 2012-01-23 15:52:55 -0500

xamox gravatar image

Yeah, numpy can handle most of that kind of thing. In general with python you want to avoid loops over large datasets wherever possible. In this case the numpy.where function is probably what you want. A very sloppy, inefficient recipie:

mycolor = (224.0, 131.0, 106.0)
coordinates = np.where(Image("lenna").colorDistance(mycolor).getNumpy()[:,:,0]
== 0)
myleftmostpixel = (coordinates[0][0], coordinates[1][0])
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2012-01-23 15:52:29 -0500

Seen: 748 times

Last updated: Jan 23 '12