I am using Python to build an image-rendering app that renders numpy arrays as images. I need a way to automate the calculation screen DPI, as I am generating images of the same size on different monitors (e.g., 2x2 inches). I am in Windows 10, Python 3.7.
I am not asking how to get the monitor's screen size -- like finding out if your monitor is 3000x2000 pixels (as was solved at: How do I get monitor resolution in Python?).
Rather, I want something that calculates DPI speficially, and takes into account any scale settings the user has applied that might change the DPI of their system. E.g., I have the scale set to 250%.
Following this answer at experts-exchange (https://www.experts-exchange.com/questions/21794611/Detecting-system-DPI-settings-in-Python.html), I have tried using this:
from ctypes import windll
def get_ppi():
LOGPIXELSX = 88
LOGPIXELSY = 90
user32 = windll.user32
user32.SetProcessDPIAware()
dc = user32.GetDC(0)
pix_per_inch = windll.gdi32.GetDeviceCaps(dc, LOGPIXELSX)
print("Horizontal DPI is", windll.gdi32.GetDeviceCaps(dc, LOGPIXELSX))
print("Vertical DPI is", windll.gdi32.GetDeviceCaps(dc, LOGPIXELSY))
user32.ReleaseDC(0, dc)
return pix_per_inch
It seems to be the right ballpark, but returns a number that is too low. It returns 240, while my actual DPI is closer to 285.
I would prefer to avoid using a GUI framework like Qt for this, as I want to minimize overhead, but if that's the only way, then I will! If I do have to use a framework, I'd prefer PyQt5.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…