【发布时间】:2020-09-23 23:29:08
【问题描述】:
我想根据显示的某些 pdf 页面的位置在屏幕上显示一些信息。为此,我需要获取页面边框的位置,但现在,我只有一个非常幼稚和低效的解决方案,它包括获取窗口的垂直切片,直到我检测到“大部分是白色”切片,我采取作为搜索的边界:
import win32gui
from win32api import GetSystemMetrics
dc = win32gui.GetDC(0)
width,height=GetSystemMetrics(0),GetSystemMetrics(1)
fraction=200 # This is to reduce the amount of info to be processed
p_height=height/fraction
suma=0 # This variable holds the number of withe pixels of each slice
for i in range(width): # while in screen w
for j in range(int(p_height)): # hile in screen h
if win32gui.GetPixel(dc, i, j*fraction)!=16777215:
pass # pass if pixel is not withe
else:
suma+=1
if suma/p_height>0.4: # if more than 40% of the pixels are white,
print(i)
break
else:
suma=0
这既慢又不聪明。我想有一种方法可以获得我正在寻找的信息,而不必从字面上“看”屏幕。有什么建议吗?
(我用谷歌浏览器打开pdf。)
【问题讨论】:
-
除非您可以通过某些 API 直接与显示 PDF 的应用程序直接交互以获取信息,否则我的猜测可能没有更好的方法。
-
您可能需要考虑更多。如果 chrome 不是全屏,可能只占屏幕的四分之一,那么你必须检测不到 40% 的屏幕白色区域(PDF 文件)。
-
如果你确定chrome是全屏的,那么我建议你使用
CreateDIBSection获取像素值,比GetPixel快。参考:Get Pixel color fastest way? -
您好,您的问题解决了吗?
-
请在问题中更新。