Hi, im quite new to python ( I know the basics and all that) but I was wondering if it was possible to somehow use python to read a colour on the screen and detect what colour is showing or show the rgb format? E.g a blue image shows on the screen and the program can detect and print the values Blue=(0,0,255) sorry if I’m being a bit unclear, I’m not the greatest at python or do I know what I’m doing XD.
Reading colours ??
Collapse
X
-
-
Here are some options.
This is a quick reference not a complete program.
If you mean "read a colour on the screen" as reading a color from some other program, then have you considered a screen capture of that area? Then chose a point on the screen capture to analyze for rgb. Then compare that rgb to the rgb that you are interested in.
Example:
In the following, set the timer for your system to allow for the screenshot to complete before saving to a file.
Now that you have a saved copy of what you are analyzing, you can directly check outside of your program to see that you grabbed the right image. Use the following or similar:Code:import pyautogui, time time.sleep(7) screenshot = pyautogui.screenshot() screenshot.save("isitblue.png")
Code:#Load and show an image with Pillow from PIL import Image #Load the image img = Image.open('isitblue.png') #verify that it is in RBG format print(img.mode) #the result should be "RGB"
Also,
Python PIL (Python Imaging Library ) and getpixel() Method
getpixel() Returns the pixel (as a single) at x, y.
Code:# Importing Image from PIL package from PIL import Image # creating an image object mypng = Image.open(r"C:\isitblue.png") mypixelcheck = mypng.load() cordinate = x, y = 10, 10 # using getpixel method print (mypng.getpixel(cordinate));
-
Includes Python's GUI automation module PyAutoGui.
To get it, enter the following command
The code gets the color of the mouse position.Code:pip install pyautogui
Move the mouse to the position where you want to get the color and press the Enter key.
Code:import pyautogui as gui import sys print('Ctrl+C to exit') try: while True: inp=input("Place the cursor on the part where you want to get the color and press the Enter key.\n") x,y = gui.position() rgb = gui.pixel(x,y) print('RGB=',rgb) # Convert to HTML color code R = 140 G = 180 B = 250 color_code = '#{}{}{}'.format(hex(R), hex(G), hex(B)) HTML_color_code = color_code.replace('0x', '') print('HTML=',HTML_color_code) except KeyboardInterrupt: print('\nStop') sys.exit()Comment
Comment