2015-10-17 44 views
2

我正試圖使用​​pyglet - 一個簡單的程序,當用戶單擊圖像時它將返回True,否則返回False。 我知道如何使這項工作,但這是我的形象: kitten
正如你所看到的 - 這個圖像有一個白色背景,我的pyglet窗口也是如此,所以我現在要做的就是讓它只返回True當用戶點擊的像素是什麼,但白色。
我有一個如何做到這一點的想法,但我需要知道的是如何檢查某個像素的顏色是什麼。獲取窗口中指定像素的顏色

#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) 
+0

你可以寫一些代碼:你怎麼顯示圖像? – lilezek

+0

@lilezek好的,我已經添加了代碼。 –

回答

0

您可以通過訪問該圖像的像素:

rawimage = kitty .get_image_data() 
format = 'RGBA' 
pitch = rawimage.width * len(format) 
pixels = rawimage.get_data(format, pitch) 

然後得到的像素X,Y使用:

pixels[kitty.width * y + x]