I'm trying to make - on pyglet - a simple program which when the user clicks on an image it returns True
, otherwise it returns False
.
I know how to make this work, but this is my image:
kitten
As you can see - this image has a white background, and so does my pyglet window, so what I want to do now is make it only return True
when the pixel which the user clicks on is anything but white.
I have an idea of how to do this, but what I need to know is how to check what the colour of a certain pixel is.
#This is getting and resizing the image
kitty = pyglet.resource.image('kitty.jpg')
kitty.width = kitty.width//4
kitty.height = kitty.height//4
#These are variables of the coordinates
#to draw the image at
kitty_draw_x = 0
kitty_draw_y = 0
#And this is the function to draw the kitty!
@window.event
def on_draw():
window.clear()
kitty.blit(kitty_draw_x, kitty_draw_y)
You can access pixels of the image using:
rawimage = kitty .get_image_data()
format = 'RGBA'
pitch = rawimage.width * len(format)
pixels = rawimage.get_data(format, pitch)
Then get the pixel x,y using:
pixels[kitty.width * y + x]