[Pythhon] 다중 모니터 환경에서의 창 캡처

의 목적


파이톤으로
· 가상 데스크톱 내 창
서브모니터 내 창
를 캡처하여 OpenCV에서 사용

필수 모듈


pywin32,numpy,cv2

코드


WindowCapture.py
import win32gui
import win32ui
import win32con
import numpy as np
import cv2


def WindowCapture(window_name: str, bgr2rgb: bool = False):
    # 現在アクティブなウィンドウ名を探す
    process_list = []

    def callback(handle, _):
        process_list.append(win32gui.GetWindowText(handle))
    win32gui.EnumWindows(callback, None)

    # ターゲットウィンドウ名を探す
    for process_name in process_list:
        if window_name in process_name:
            hnd = win32gui.FindWindow(None, process_name)
            break
    else:
        # 見つからなかったら画面全体を取得
        hnd = win32gui.GetDesktopWindow()

    # ウィンドウサイズ取得
    x0, y0, x1, y1 = win32gui.GetWindowRect(hnd)
    width = x1 - x0
    height = y1 - y0
    # ウィンドウのデバイスコンテキスト取得
    windc = win32gui.GetWindowDC(hnd)
    srcdc = win32ui.CreateDCFromHandle(windc)
    memdc = srcdc.CreateCompatibleDC()
    # デバイスコンテキストからピクセル情報コピー, bmp化
    bmp = win32ui.CreateBitmap()
    bmp.CreateCompatibleBitmap(srcdc, width, height)
    memdc.SelectObject(bmp)
    memdc.BitBlt((0, 0), (width, height), srcdc, (0, 0), win32con.SRCCOPY)

    # bmpの書き出し
    if bgr2rgb is True:
        img = np.frombuffer(bmp.GetBitmapBits(True), np.uint8).reshape(height, width, 4)
        img = cv2.cvtColor(img, cv2.COLOR_bgr2rgb)
    else:
        img = np.fromstring(bmp.GetBitmapBits(True), np.uint8).reshape(height, width, 4)

    # 後片付け
    # srcdc.DeleteDC()
    memdc.DeleteDC()
    # win32gui.ReleaseDC(hnd, windc)
    win32gui.DeleteObject(bmp.GetHandle())

    return img

반환값


numpy.ndaray (창 너비, 높이, 4 (채널 수)

사용 예

img = WindowCapture("メモ帳") # 部分一致
cv2.imshow("", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

결실



참고 자료


python 캡처 올바르게 사용(Windows)
http://e-yuki67.hatenablog.com/entry/2017/02/12/152759
BitBlt 함수
https://docs.microsoft.com/en-us/windows/desktop/api/wingdi/nf-wingdi-bitblt
[해외 사이트] 파이썬 윈32ui.CreateDCFromHandle() Examples
https://www.programcreek.com/python/example/74480/win32ui.CreateDCFromHandle

좋은 웹페이지 즐겨찾기