Easy PIL question

Discussion in 'Python' started by Adam W., Feb 16, 2008.

  1. Adam W.

    Adam W. Guest

    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)
    Adam W., Feb 16, 2008
    #1
    1. Advertising

  2. Adam W.

    Gary Herron Guest

    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
    Gary Herron, Feb 16, 2008
    #2
    1. Advertising

  3. Adam W.

    Guest

    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
    , Feb 16, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hazzard
    Replies:
    2
    Views:
    634
    Hazzard
    Apr 6, 2004
  2. news.west.cox.net

    PIL question - saving JPEG

    news.west.cox.net, Oct 10, 2004, in forum: Python
    Replies:
    5
    Views:
    4,483
    Christos TZOTZIOY Georgiou
    Oct 14, 2004
  3. Easy PIL Question?

    , Oct 30, 2006, in forum: Python
    Replies:
    2
    Views:
    321
  4. Bruno Desthuilliers
    Replies:
    5
    Views:
    381
    Bruno Desthuilliers
    Aug 29, 2007
  5. Sverre
    Replies:
    2
    Views:
    3,543
    Sverre
    Dec 17, 2009
Loading...

Share This Page