Easy PIL question

A

Adam W.

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)
 
G

Gary Herron

Adam said:
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
 
B

bearophileHUGS

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top