码迷,mamicode.com
首页 > 编程语言 > 详细

python 调用windowsAPI截图

时间:2020-04-24 11:39:39      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:截取   copy   div   waitkey   截图   rom   text   高度   created   

方式一

import time
import win32gui, win32ui, win32con, win32api


def window_capture(filename):
    hwnd = 0  # 窗口的编号,0号表示当前活跃窗口
    # 根据窗口句柄获取窗口的设备上下文DC(Divice Context)
    hwndDC = win32gui.GetWindowDC(hwnd)
    # 根据窗口的DC获取mfcDC
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    # mfcDC创建可兼容的DC
    saveDC = mfcDC.CreateCompatibleDC()
    # 创建bigmap准备保存图片
    saveBitMap = win32ui.CreateBitmap()
    # 获取监控器信息
    MoniterDev = win32api.EnumDisplayMonitors(None, None)
    w = MoniterDev[0][2][2]
    h = MoniterDev[0][2][3]
    # print w,h   #图片大小
    # 为bitmap开辟空间
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    # 高度saveDC,将截图保存到saveBitmap中
    saveDC.SelectObject(saveBitMap)
    # 截取从左上角(0,0)长宽为(w,h)的图片
    saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)
    saveBitMap.SaveBitmapFile(saveDC, filename)


beg = time.time()
for i in range(10):
    window_capture("haha.jpg")
end = time.time()
print(end - beg)

方式二

import time
from PIL import ImageGrab, Image
import numpy as np
import cv2

beg = time.time()
debug = False
# img = ImageGrab.grab(bbox=(250, 161, 1141, 610))
img = ImageGrab.grab()
end = time.time()
print(‘time:‘,end - beg)

img.show()
img.save("screen.jpg")

# PIL image to OpenCV image
im = np.array(img)
cv2.imshow(‘cv image‘, im)
cv2.waitKey()

# OpenCV image to PIL image
im_pil = Image.fromarray(im)
im_pil.show()

python 调用windowsAPI截图

标签:截取   copy   div   waitkey   截图   rom   text   高度   created   

原文地址:https://www.cnblogs.com/se7enjean/p/12766269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!