【问题标题】:Screenshot on multiple monitor setup pyautogui多显示器设置 pyautogui 上的屏幕截图
【发布时间】:2020-12-23 12:12:15
【问题描述】:

我在 python 中使用 pyautogui 来截屏

import pyautogui
screen = pyautogui.screenshot()
screen.save("file.jpg")

它可以在所有平台的单个屏幕上正常工作。但在多屏幕系统中,它将两个屏幕组合在一个屏幕截图中。但我想要一个当前正在使用的监视器的屏幕截图。

【问题讨论】:

  • 有一个可选的region 参数。与some screen info 一起,您应该能够实现您想要的。请注意,某些屏幕的位置可能为负数。

标签: python python-3.x linux windows macos


【解决方案1】:

找到解决方案here

from PIL import ImageGrab
from functools import partial
ImageGrab.grab = partial(ImageGrab.grab, all_screens=True)

pyautogui.screenshot() 将捕获所有屏幕。

【讨论】:

  • 似乎不起作用。
【解决方案2】:

如果您访问官方文档网站,您会看到:

问:PyAutoGUI 是否适用于多显示器设置。

答:不,目前 PyAutoGUI 只处理主监视器。

您可以为此使用 mss 模块。

from mss import mss
#One screenshot per monitor:

for filename in screenshotter.save():
    print(filename)

#Screenshot of the monitor 1:

for filename in screenshotter.save(screen=1):
    print(filename)

#Screenshot of the monitor 1, with callback:

def on_exists(fname):
    ''' Callback example when we try to overwrite an existing
        screenshot.
    '''
    from os import rename
    from os.path import isfile
    if isfile(fname):
        newfile = fname + '.old'
        print('{0} -> {1}'.format(fname, newfile))
        rename(fname, newfile)
    return True

for filename in screenshotter.save(screen=1, callback=on_exists):
    print(filename)

#A screenshot to grab them all:

for filename in screenshotter.save(output='fullscreen-shot.png', screen=-1):
    print(filename)

【讨论】:

    【解决方案3】:

    mss包可以轻松解决这个问题

    1.安装包

    pip install mss

    2.截图

    ? 第一台显示器 ?

    import os
    import os.path
    
    import mss
    
    
    def on_exists(fname):
        # type: (str) -> None
        """
        Callback example when we try to overwrite an existing screenshot.
        """
    
        if os.path.isfile(fname):
            newfile = fname + ".old"
            print("{} -> {}".format(fname, newfile))
            os.rename(fname, newfile)
    
    
    with mss.mss() as sct:
        filename = sct.shot(output="mon-{mon}.png", callback=on_exists)
        print(filename)
    

    ? 第二监视器 ?

    import mss
    import mss.tools
    
    
    with mss.mss() as sct:
        # Get information of monitor 2
        monitor_number = 2
        mon = sct.monitors[monitor_number]
    
        # The screen part to capture
        monitor = {
            "top": mon["top"],
            "left": mon["left"],
            "width": mon["width"],
            "height": mon["height"],
            "mon": monitor_number,
        }
        output = "sct-mon{mon}_{top}x{left}_{width}x{height}.png".format(**monitor)
    
        # Grab the data
        sct_img = sct.grab(monitor)
    
        # Save to the picture file
        mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
        print(output)
    

    ? 与 OpenCV 一起使用 ?

    import mss
    import cv2
    import numpy as np
    
    with mss.mss() as sct:
        
        # Get information of monitor 2
        monitor_number = 2
        mon = sct.monitors[monitor_number]
    
        # The screen part to capture
        monitor = {
            "top": mon["top"],
            "left": mon["left"],
            "width": mon["width"],
            "height": mon["height"],
            "mon": monitor_number,
        }
        output = "sct-mon{mon}_{top}x{left}_{width}x{height}.png".format(**monitor)
    
        # Grab the data
        sct_img = sct.grab(monitor)
        img = np.array(sct.grab(monitor)) # BGR Image
        
        # Display the picture
        cv2.imshow("OpenCV", img)
        cv2.waitKey(0)
    

    3.重要说明

    • sct.monitors 将包含多个项目,即使您只有一个监视器,因为第一个项目将是组合屏幕
    >>> sct.monitors # if we have a single monitor
    [{'left': 0, 'top': 0, 'width': 1366, 'height': 768}, 
    {'left': 0, 'top': 0, 'width': 1366, 'height': 768}]
    
    >>> sct.monitors # if we have two monitors
    [{'left': 0, 'top': 0, 'width': 3286, 'height': 1080}, 
    {'left': 1920, 'top': 0, 'width': 1366, 'height': 768}, 
    {'left': 0, 'top': 0, 'width': 1920, 'height': 1080}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多