0

When I use PIL.ImageGrab.grab() on my Windows 10 PC, I get exactly the RGB colors I expect. For example, (255, 0, 0) for red. On my Mac, PIL.ImageGrab.grab() returns a PIL.PngImagePlugin.PngImageFile which has unexpected RGBA values, e.g. (235, 50, 22, 255) for red. That converts, as expected with an A value of 255, to (235, 50, 22).

Can anyone explain how to get the true RGBA/RGB color values on a Mac?

My test program:

from pynput import mouse
from PIL import ImageGrab

class MouseHandler:
    def on_mouse_click(self, x, y, button, pressed):
        if pressed:
            image_rgba = ImageGrab.grab()
            image_rgb = image_rgba.convert('RGB')
            print(f'{x, y} -> rgba={image_rgba.getpixel((x, y))} rgb={image_rgb.getpixel((x, y))}')

if __name__ == "__main__":
    handler = MouseHandler();
    with mouse.Listener(on_click=handler.on_mouse_click) as ml:
        ml.join()

My test color source: the red and blue blocks on https://www.w3schools.com/html/html_colors_rgb.asp

My test program's output on my Mac, clicking on the red block, then the blue block:

(1234.27734375, 782.59765625) -> rgba=(235, 50, 22, 255) rgb=(235, 50, 22)

(1752.3125, 796.7890625) -> rgba=(28, 0, 255, 255) rgb=(28, 0, 255)

I was expecting (255, 0, 0) and (0, 0, 255).

I'm running this test on a 2021 MacBook Pro running MacOS 13.4.1, with Python 3.11 and Pillow 10.0.0.

(P.S., I've also tried using mss, and get the same results.)

  • Probably because Macs use a larger color space than sRGB, and somebody's doing a color space conversion somewhere. Sorry I don't have details or I'd leave an answer. – Mark Ransom Jul 07 '23 at 02:33

0 Answers0