colors - Python / PyGame: Find pixel by colorcode -


is there way find pixel (inside surface / image) color? like:

img = python.image.load("image.gif").convert() img.find((255, 255, 255)) >> (50, 100) = white pixel 

if don't know mean, feel free ask. thank you!

def findpixel(img, r, g, b):     x in range(0, img.get_width()):         y in range(0, img.get_height()):             pixel = img.get_at((x, y))             if pixel[0] >= r , pixel[1] >= g , pixel[2] >= b:                 return pixel     return none 

this written of top of head. passing in image object should word. if not you'll have input image.surface object reference. idea of iterating on x , y should work in theory.

pygame host no function this, supply ability or iterate on pixel positions.

there faster way , store entire image-array prior loop , iterate on array instead of calling get_at function believe, don't use pygame these days can't test optimization difference of 2 implementations i'll leave @ , leave optimization you.

if you're interested in finding color values corresponding parameters (thanks superbiasedman):

def findpixel(img, r, g, b):     found = []     x in range(0, img.width):         y in range(0, img.height):             pixel = img.get_at((x, y))             if pixel[0] >= r , pixel[1] >= g , pixel[2] >= b:                 found.append((x, y))     return found 

note slower, you'll find pixels in 1 iteration.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -