计算屏幕DPI [英] Calculate screen DPI

查看:258
本文介绍了计算屏幕DPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python构建一个图像渲染应用程序,该程序将numpy数组呈现为图像。我需要一种自动计算屏幕DPI的方法,因为我正在不同的显示器(例如2x2英寸)上生成相同大小的图像。我使用的是Windows 10,Python 3.7。

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.

我不是不是在询问如何获取显示器的屏幕尺寸- -像找出显示器是否为3000x2000像素(已在以下位置解决:如何在Python中获得监视器分辨率?)。

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?).

我希望有一些专门计算DPI并考虑任何比例的东西用户应用的设置可能会更改其系统的DPI。例如,我将比例设置为250%。

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%.

在experts-exchange( https://www.experts-exchange.com/questions/21794611/Detecting-system-DPI-settings-in-Python.html ),我曾尝试使用以下方法:

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似乎是正确的选择,但返回的数字太低。它返回240,而我的实际DPI却接近285。

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.

我希望避免使用Qt这样的GUI框架,因为我想最大程度地减少开销,但是如果那是唯一的方法,那我会的!如果确实需要使用框架,则我更喜欢PyQt5。

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.

推荐答案

虽然我说我想避免使用它,是使用PyQt5实现这一目标的一种非常简单的方法。我考虑得越多,就越可能是最好的解决方案,因为它在很大程度上与平台无关:

While I said I wanted to avoid it, there is one very simple way to pull this off using PyQt5. The more I think about it, the more I think this could be the best solution, as it is largely platform independent:

import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
screen = app.screens()[0]
dpi = screen.physicalDotsPerInch()
app.quit()

请注意, app.screens()返回屏幕列表。就我而言,我只连接了一个屏幕,但是您可能有多个屏幕,因此请务必注意需要从哪个屏幕获取dpi。而且,如果将所有这些内容都包含在一个函数中,它将不会因PyQt垃圾而弄乱您的名称空间。

Note that app.screens() returns a list of screens. In my case I only have one attached but you may have multiple, so be sure to be aware of which screen you need to get dpi from. And if you keep all this contained in a function, it won't clutter your namespace with PyQt junk.

此外,有关QScreen的更多信息( sc 是QScreen对象),请参见此文档页面:

https://doc.qt.io/qt-5/qscreen.html

Also, for more on QScreen (sc is a QScreen object) see this doc page:
https://doc.qt.io/qt-5/qscreen.html

您可以从中获取各种有趣的东西

There's all sorts of cool stuff you can pull from it.

这篇关于计算屏幕DPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆