如何解决如何获取屏幕上显示的pdf页面的位置?
我想根据显示的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
这既缓慢又不明智。我猜想有一种方法可以获取我正在寻找的信息,而不必从字面上“看”屏幕。有什么建议吗?
(我使用Google Chrome浏览器打开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
step=10 # Here,the code will look at one of every ten vertical slices
p_height=height/fraction
suma=0 # This variable holds the number of withe pixels of each slice
for i in range(0,width,step): # while in screen w
for j in range(int(p_height)): # while 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
suma=0
for k in range(step): # to cover the ignored slices
for j in range(int(p_height)): # while in screen h
if win32gui.GetPixel(dc,(i-k),j*fraction)!=16777215:
pass # pass if pixel is not withe
else:
suma+=1
if suma/p_height<0.4: # If less than 40% of pixels are white,we found the edge
print(i-k)
break
else:
suma=0
break
else:
suma=0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。