Get screen information

You can use Pywin32 to get screen size and pixel information.


Get screen resolution

from win32api import GetSystemMetrics

print('Width:', GetSystemMetrics(0))
print('Height:', GetSystemMetrics(1))
Width: 1536
Height: 864

You can see that the screen has a resolution of 1536px and 864px.


Get screen pixel color

import win32api
import win32gui

color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), 500, 500)
print(hex(color))

GetPixel() in win32gui lets you get information about the color of a particular pixel on the screen.

In the example above, pixel information in x=500 and y=500 is printed in hexadecimal form.


If you want to print the color as an RGB tuple,

import win32api
import win32gui


def rgbint2rgbtuple(RGBint):

    blue = RGBint & 255
    green = (RGBint >> 8) & 255
    red = (RGBint >> 16) & 255

    return (red, green, blue)

color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), 500, 500)
print(rgbint2rgbtuple(color))

You can convert it using a function.

Prev