Python Forum
Making a colour field
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a colour field
#2
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()
Reply


Messages In This Thread
Making a colour field - by Leo_Red - Jan-20-2021, 01:48 PM
RE: Making a colour field - by BashBedlam - Jan-21-2021, 03:49 AM
RE: Making a colour field - by Leo_Red - Jan-21-2021, 05:32 AM
RE: Making a colour field - by BashBedlam - Jan-21-2021, 02:33 PM
RE: Making a colour field - by Leo_Red - Jan-21-2021, 03:50 PM
RE: Making a colour field - by BashBedlam - Jan-21-2021, 05:31 PM
RE: Making a colour field - by Leo_Red - Jan-22-2021, 05:37 AM
RE: Making a colour field - by BashBedlam - Jan-22-2021, 02:05 PM
RE: Making a colour field - by Leo_Red - Jan-22-2021, 03:27 PM
RE: Making a colour field - by BashBedlam - Jan-22-2021, 04:00 PM
RE: Making a colour field - by Leo_Red - Jan-22-2021, 04:07 PM
RE: Making a colour field - by BashBedlam - Jan-22-2021, 04:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't change the colour of Tk button text Pilover 6 15,092 Nov-15-2022, 10:11 PM
Last Post: woooee
  [Tkinter] changing the frame colour nick123 4 16,789 Jul-24-2020, 07:32 AM
Last Post: LegacyCoding

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020