Python猎取电脑截图有多种形式,详细以下:
PIL中的ImageGrab模块
windows API
PyQt
pyautogui
PIL中的ImageGrab模块
import time import numpy as np from PIL import ImageGrab img = ImageGrab.grab(bbox=(100, 161, 1141, 610)) img = np.array(img.getdata(), np.uint8).reshape(img.size[1], img.size[0], 3)
应用PIL中的ImageGrab模块简略,然而效率有点低。
windows API
挪用windows API,速率快然而应用较复杂,这里就没有做具体引见了,由于有更好用的PyQt。
PyQt
PyQt比挪用windows API简略不少,并且有windows API的不少劣势,比方速率快,能够指定猎取的窗口,即便窗口被遮挡。需留意的是,窗口最小化时无奈猎取截图。
起首需求猎取窗口的句柄。
import win32gui hwnd_title = dict() def get_all_hwnd(hwnd,mouse): if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd): hwnd_title.update({hwnd:win32gui.GetWindowText(hwnd)}) win32gui.EnumWindows(get_all_hwnd, 0) for h,t in hwnd_title.items(): if t is not "": print(h, t)
顺序会打印窗口的hwnd以及title,有了title就能够进行截图了。
from PyQt5.QtWidgets import QApplication from PyQt5.QtGui import * import win32gui import sys hwnd = win32gui.FindWindow(None, 'C:\Windows\system32\cmd.exe') app = QApplication(sys.argv) screen = QApplication.primaryScreen() img = screen.grabWindow(hwnd).toImage() img.save("screenshot.jpg")
pyautogui
pyautogui是比拟简略的,然而不克不及指定猎取顺序的窗口,因而窗口也不克不及遮挡,不外能够指定截屏的地位,0.04s一张截图,比PyQt稍慢一点,但也很快了。
import pyautogui import cv2 img = pyautogui.screenshot(region=[0,0,100,100]) # x,y,w,h # img.save('screenshot.png') img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
更多Python相干技巧文章,请拜访Python教程栏目进行学习!
以上就是python若何进行截图的具体内容,更多请存眷资源魔其它相干文章!
标签: python教程 python编程 python使用问题 python如何截图
版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。
抱歉,评论功能暂时关闭!