像风铃那样 2022-12-03 22:55 采纳率: 76.9%
浏览 16
已结题

多显示器电脑截图时出现异常

我这台电脑有两个显示器,日常使用的是主显示器。编写此程序是为了实时监控另一显示器的状况,但遇到了一些问题。


"""
此程序旨在将另一个显示器的内容以窗口的形式显示在主显示器(打开该窗口的屏幕)上。
@File  : monitor.py
@Author: syf
@CSDN  : 稻草的使命
@Time  : 2022/12
"""
import cv2 as cv
import pyautogui
from PIL import ImageGrab
import numpy


def grab():
    image = pyautogui.screenshot(region=[0, 0, 1920, 1080])
    return image

def show(im):
    # im = numpy.array(im.getdata(), numpy.uint8).reshape(im.size[1], im.size[0], 3)
    img = cv.cvtColor(numpy.asarray(im), cv.COLOR_RGB2BGR)
    cv.imshow("monitor", img)
    cv.waitKey(500)

def save(frame):
    pass


if __name__ == '__main__':
    while True:
        show(grab())

运行结果一切正常,但当我尝试image = pyautogui.screenshot(region=[-100, 0, 50, 50])时,我想要得到另一显示器上的内容,结果窗口内部一片漆黑,什么也没有。

我尝试了各种方法,试遍各个模块的截图功能,但都只能获取主显示器上的内容。请问该如何解决,谢谢!

  • 写回答

1条回答 默认 最新

  • ShowMeAI 2022-12-04 00:03
    关注

    使用mss完成多屏幕截图

    可以通过pip install mss安装mss工具库。

    第1块屏幕

    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)
    

    第2块屏幕

    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对第2块屏幕截图

    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)
    

    补充点

    sct.monitors # 如果只有一块屏幕
    [{'left': 0, 'top': 0, 'width': 1366, 'height': 768}, 
    {'left': 0, 'top': 0, 'width': 1366, 'height': 768}]
    
    sct.monitors # 如果有2块屏幕
    [{'left': 0, 'top': 0, 'width': 3286, 'height': 1080}, 
    {'left': 1920, 'top': 0, 'width': 1366, 'height': 768}, 
    {'left': 0, 'top': 0, 'width': 1920, 'height': 1080}]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月4日
  • 已采纳回答 12月4日
  • 创建了问题 12月3日

悬赏问题

  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案