【问题标题】:MacOS: screenshot a minimised window with PythonMacOS:使用 Python 截取最小化窗口
【发布时间】:2019-07-15 14:28:00
【问题描述】:

我想通过从屏幕截图中读取最小化(聊天)应用程序的某些状态。

我知道如何使用screencapture 截取可见窗口的屏幕截图,但我不知道如何将其推广到最小化窗口。这似乎是一个适用于 Windows 的示例:Python Screenshot of inactive window PrintWindow + win32gui

是否有 MacOS 等价物?

编辑:我的问题不是上面链接的问题的重复,因为我需要的不仅仅是win32 等价物,我还需要屏幕截图功能。

【问题讨论】:

标签: python-3.x screenshot


【解决方案1】:

我创建了一个最小的工作示例:

from Quartz import CGWindowListCopyWindowInfo, kCGNullWindowID, kCGWindowListOptionAll
import matplotlib.pyplot as plt
from PIL import Image
import os
from uuid import uuid4

gen_filename = lambda : str(uuid4())[-10:] + '.jpg'

def capture_window(window_name):
    window_list = CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID)
    for window in window_list:
        try:
            if window_name.lower() in window['kCGWindowName'].lower():
                filename = gen_filename()
                os.system('screencapture -l %s %s' %(window['kCGWindowNumber'], filename))
                img = Image.open(filename)
                plt.imshow(img)
                plt.xticks([])
                plt.yticks([])
                os.remove(filename)
                break
        except:
            pass
    else:
        raise Exception('Window %s not found.'%window_name)

它使用screencapture 将窗口截图保存到一个临时文件,然后在 Python 中重新加载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多