【问题标题】:How to get the position of a pdf page shown on screen?如何获取屏幕上显示的pdf页面的位置?
【发布时间】: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?
  • 您好,您的问题解决了吗?
  • 请在问题中更新。

标签: python pdf winapi


【解决方案1】:

为了让这个问题一直存在,这里对我的原始代码进行了一些改进,以节省一些时间。实际上,代码找到页面需要一秒钟以上的时间,这是不可接受的。

我刚刚介绍了一个“步骤”来忽略一些屏幕切片,以便更快地找到屏幕中大部分为白色的部分,然后慢慢返回以找到该部分的确切起点。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多