How do I get monitor resolution in Python?
In Windows, you can also use ctypes with GetSystemMetrics()
:
import ctypesuser32 = ctypes.windll.user32screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
so that you don't need to install the pywin32 package;it doesn't need anything that doesn't come with Python itself.
For multi-monitor setups, you can retrieve the combined width and height of the virtual monitor:
import ctypesuser32 = ctypes.windll.user32screensize = user32.GetSystemMetrics(78), user32.GetSystemMetrics(79)
I created a PyPI module for this reason:
pip install screeninfo
The code:
from screeninfo import get_monitorsfor m in get_monitors(): print(str(m))
Result:
monitor(1920x1080+1920+0)monitor(1920x1080+0+0)
It supports multi monitor environments. Its goal is to be cross platform; for now it supports Cygwin and X11 but pull requests are totally welcome.
On Windows:
from win32api import GetSystemMetricsprint("Width =", GetSystemMetrics(0))print("Height =", GetSystemMetrics(1))
If you are working with high resolution screen, make sure your python interpreter is HIGHDPIAWARE.
Based on this post.