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.
- here's references
get_width()etc you'll need use - a useful feature speed optimization
get_rect()
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
Post a Comment