473,378 Members | 1,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
3 2823
SwissProgrammer
220 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
This is a good color to read this font.
Dec 21 '20 #3
SioSio
272 256MB
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()
Dec 22 '20 #4

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

Similar topics

4
by: Luca | last post by:
Hello Everybody, I'm a 26 years old Italian "Florentine" Computer technician :) I'm writing you about an idea that I've got of a function that could be introduced in new web browsers (or even...
4
by: Els | last post by:
Hi, I would like an opinion on the following: I have a page which is made up of background-images with transparent linked images in front of it, which on hover show text in CSS popups. Due to...
3
by: CSX | last post by:
Hi I have just loaded Visual Basic.Net. I find the interfact colours very drab, especially compared to Office 2003. How do I change the interface colours (currently greys & white) Ta Carlo
3
by: Peter Williams | last post by:
Hi All, I want to write some javascript for a html page which does the following. Imagine that the page contains a table with 2 columns and 3 rows, e.g.: +---+---+ | A | B | +---+---+
8
by: Doug Laidlaw | last post by:
I tried to grab an image from a Web page the other day. It turned out that the page was made up of three horizontal bands, and part of the image was in each. One band was a JPEG, another was a...
4
by: Tim Marshall | last post by:
The report indicated in the send object command below has various colours in it which show nicely when the report is previewed and printed. However, when sent as an attachment to users without...
0
by: Rui Oliveira | last post by:
I am using a tree from CTreeCtrl. To create the imagelist I am using a bitmap with 256 colours. But the icons in my tree only appear with 16 colours. What is supposed to do to have icons...
9
by: piyush2884 | last post by:
hi guys, got anything on this issue?
5
by: Jameson | last post by:
Hi, I have a list of known colours, generated using: Dim colorNames As New System.Collections.Generic.List(Of String) For Each known As KnownColor In .GetValues(GetType(KnownColor)) Dim...
2
by: Sodrohu | last post by:
Currently I got a thermal image mapping program which converts grayscale pictures to RGB. Now, what I need to do is to convert those 3 RGB colors into other three colours. The choice of colours are...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.