Jan-21-2021, 03:49 AM
(This post was last modified: Jan-21-2021, 03:49 AM by BashBedlam.)
This code originally ran in python2 but it still works. Get a picture of the color field of your choice and name it img.bmp (converting to bmp of course) or change the line :
image = pygame.image.loadi ("./img.bmp")to whatever the name of your image is. Put it in the same folder with this and run the code. It will print out the RGBA (RGB plus Alpha) of the pixel that you clicked on. Hope this helps.
import sys, pygame pygame.init () size = width, height = 320, 240 screen = pygame.display.set_modei (size) image = pygame.image.load ("./img.bmp") image_rect = image.get_rect() screen.fill ((0,0,0)) screen.blit (image, image_rect) screensurf = pygame.display.get_surface () looking_for_color = True while looking_for_color: for event in pygame.event.get (): if event.type == pygame.QUIT : looking_for_color = False if event.type == pygame.MOUSEBUTTONDOWN : mouse = pygame.mouse.get_pos() pxarray = pygame.PixelArray(screensurf) pixel = pygame.Color(pxarray[mouse[0],mouse[1]]) print (pixel) print (screensurf.get_at(mouse)) pygame.display.flip()