Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Easy PIL question (http://www.velocityreviews.com/forums/t592128-easy-pil-question.html)

 Adam W. 02-16-2008 04:35 PM

Easy PIL question

I know there is an easy way to do this, but I can't figure it out, how
do I get the color of a pixel? I used the ImageGrab method and I want
to get the color of a specific pixel in that image. If you know how
to make it only grab that pixel, that would also be helpful.
Basically I'm trying to make a:
if pixel == color:
do_this()
else:
pass

And have it do this as fast as my pc can handle (that is why only
grabbing 1px would be helpful)

 Gary Herron 02-16-2008 05:14 PM

Re: Easy PIL question

Adam W. wrote:
> I know there is an easy way to do this, but I can't figure it out, how
> do I get the color of a pixel? I used the ImageGrab method and I want
> to get the color of a specific pixel in that image. If you know how
> to make it only grab that pixel, that would also be helpful.
> Basically I'm trying to make a:
> if pixel == color:
> do_this()
> else:
> pass
>
> And have it do this as fast as my pc can handle (that is why only
> grabbing 1px would be helpful)
>

Try image.getpixel((x,y)) to retrieve the pixel at (x,y).

Gary Herron

 bearophileHUGS@lycos.com 02-16-2008 08:15 PM

Re: Easy PIL question

Gary Herron:
> Try image.getpixel((x,y)) to retrieve the pixel at (x,y).

If the OP needs to access many pixels, then he can use the load()
method on the image object, and then read/write pixels (tuples of 3
ints) using getitem []

import Image
im = Image....
img = im.load()
img[x,y] = ...
.... = img[x,y]

I don't know why the image object itself can't have the __getitem__/
__setitem__

Bye,
bearophile

 All times are GMT. The time now is 09:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.