467,166 Members | 1,044 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,166 developers. It's quick & easy.

Reading colours ??

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.
Dec 16 '20 #1
  • viewed: 2409
Share:
3 Replies
SwissProgrammer
128KB
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.

Expand|Select|Wrap|Line Numbers
  1. import pyautogui, time
  2. time.sleep(7)
  3.  
  4. screenshot = pyautogui.screenshot()
  5. screenshot.save("isitblue.png")
  6.  
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:
Expand|Select|Wrap|Line Numbers
  1. #Load and show an image with Pillow
  2. from PIL import Image
  3.  
  4. #Load the image
  5. img = Image.open('isitblue.png')
  6.  
  7. #verify that it is in RBG format
  8. print(img.mode)
  9. #the result should be "RGB"
  10.  

Also,

Python PIL (Python Imaging Library ) and getpixel() Method

getpixel() Returns the pixel (as a single) at x, y.

Expand|Select|Wrap|Line Numbers
  1. # Importing Image from PIL package  
  2. from PIL import Image 
  3.  
  4. # creating an image object 
  5. mypng = Image.open(r"C:\isitblue.png") 
  6. mypixelcheck = mypng.load() 
  7. cordinate = x, y = 10, 10
  8.  
  9. # using getpixel method 
  10. print (mypng.getpixel(cordinate)); 
Dec 16 '20 #2
Catarryna
This is a good color to read this font.
4 Weeks Ago #3
128KB
Includes Python's GUI automation module PyAutoGui.
To get it, enter the following command
Expand|Select|Wrap|Line Numbers
  1. pip install pyautogui
The code gets the color of the mouse position.
Move the mouse to the position where you want to get the color and press the Enter key.
Expand|Select|Wrap|Line Numbers
  1. import pyautogui as gui
  2. import sys
  3.  
  4. print('Ctrl+C to exit')
  5.  
  6. try:
  7.     while True:
  8.        inp=input("Place the cursor on the part where you want to get the color and press the Enter key.\n")
  9.        x,y = gui.position()
  10.        rgb = gui.pixel(x,y)
  11.        print('RGB=',rgb)
  12.  
  13.        # Convert to HTML color code
  14.        R = 140
  15.        G = 180
  16.        B = 250
  17.        color_code = '#{}{}{}'.format(hex(R), hex(G), hex(B))
  18.        HTML_color_code = color_code.replace('0x', '')
  19.        print('HTML=',HTML_color_code)
  20.  
  21. except KeyboardInterrupt:
  22.     print('\nStop')
  23.     sys.exit()
4 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by CSX | last post: by
8 posts views Thread by Doug Laidlaw | last post: by
4 posts views Thread by Tim Marshall | last post: by
reply views Thread by Rui Oliveira | last post: by
9 posts views Thread by piyush2884@gmail.com | last post: by
5 posts views Thread by Jameson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.