1

I want to get the RGB value of the current mouse position on my macbook screen. Right now it is not showing correct: When I hover my mouse over a black image the RGB values don't go near (0, 0, 0) it stays like (181, 201, 233). When I put my mouse cursor on a playing YT video the RGB values do not change. Only When I move my mouse the RGB values change, but not accordingly. I have tried these functions in IDLE/new file:

1.
pyautogui.displayMousePosition()   #output:  X:  183 Y:  291 RGB: (174, 188, 211)

2.
posXY = pyautogui.position() 
print(pyautogui.pixel(posXY[0], posXY[1]))   #output:  RGB(red=200, green=212, blue=224)

3.
im = pyautogui.screenshot()
pxRGB = im.getpixel((PosXY[0], PosXY[1]))   #output:  (181, 203, 229, 255)

4.
pxRGB = ImageGrab.grab().getpixel((PosXY[0], PosXY[1])) #output:  (167, 192, 225, 255)


All of these ouputs were rong, But what did work was downloading a black-white image from internet opening it and getting the pixel value from it:

im = Image.open("black-white.jpg").convert('RGB')
px = im.getpixel((PosXY[0], PosXY[1])) 
print(px)                                     #output (0, 0, 0) and (255, 255, 255) working!

now I tried to do the same with a screenshot from my laptop (in code and out code) and using that img but that still didn't gave me correct RGB values. (even if the mouse position was incorrect I couldn't find a point darker then like (50, 46, 76) while there were plenty of them on the SC)

Any help would really be appreciated!

extra information: Macbook pro Catalina 10.15.7 I tried it first on Python 3.8 and then 3.9 The mouse positions were showed correct, left top (0, 0) and right bottom (1679, 1049). Pyautogui.displayMousePosition() is giving on my windows 7/10 laptops correct RGB values. I see that the Screenshots that were made with pyautogui.screenshot() only show my backbroundimg and not open windows like internet or some files. But 'not in code' Sc are looking normal they have though not .png assigned to them by default just empty: examplescreenshot12-23-3 Also all the code that I could find online didn't work for me. To be able to see any RGB values from the function I had to import first sys and then setting sys.platform = darwin to sys.platform = '_' I'm not working in a virtual environment and not experienced in python. I do not really care how long it will take to get the pixel RGB value. Changing backgroundImg on my macbook does result in different outputs but still not correct.

2 Answers2

1

I sat on it for 5 hours because

x, y = pyautogui.position()
pix = pyautogui.pixel(x, y)
print("str(pix)")

Did not output the correct RGB colors.

My solution:

When I am with the mouse cursor to the very bottom right of my screen and print out the X and Y coodrinates, I found that it is half, as opposed to when I take a screenshot. I.e. If I look at x=100 and y=100 on my screen, I would have to look at x=200 and y=200 in the screenshot, so double that. Here is my fix to output my mouse position and respective RGB Values in the terminal:

import pyautogui, sys
import sys
sys.platform = '_'

print('Press Ctrl-C to quit.')
try:
    while True:
        x, y = pyautogui.position()
        pix = pyautogui.pixel(x*2, y*2)

        positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4) + str(pix) 
        print(positionStr, end='')
        print('\b' * len(positionStr), end='', flush=True)
        
except KeyboardInterrupt:
    print('\n')

If this doesn't work for you, see how many pixels your screenshot has and what is the highest number when you are at the bottom right of the screen. Depending on that you have to adjust the 2:

pix = pyautogui.pixel(x*2, y*2)
-1

you can do this very easily you should insall the pyscreeze module

import pyscreeze

#the value of pixel whose value is to be get
x=23
y=23

#screen object
screen=pyscreeze.screenshot()

rgb_values=screen.getpixel((x,y))