[python][原创]全屏截图的4种方法
Posted FL1623863129
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[python][原创]全屏截图的4种方法相关的知识,希望对你有一定的参考价值。
第一种:PIL
import time
import numpy as np
from PIL import ImageGrab
import cv2
beg = time.time()
img = ImageGrab.grab(bbox=None)
img = np.array(img.getdata(), np.uint8).reshape(img.size[1], img.size[0], 3)
end = time.time()
print(end - beg)
cv2.imshow('result',img)
cv2.waitKey(0)
第二种:windows api
import cv2
import win32gui, win32ui, win32con, win32api
hwin = win32gui.GetDesktopWindow()
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
hwindc = win32gui.GetWindowDC(hwin)
srcdc = win32ui.CreateDCFromHandle(hwindc)
memdc = srcdc.CreateCompatibleDC()
bmp = win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(srcdc, width, height)
memdc.SelectObject(bmp)
memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
signedIntsArray = bmp.GetBitmapBits(False)
img = np.array(signedIntsArray).astype(dtype="uint8") # This is REALLY slow!
img.shape = (height,width,4)
srcdc.DeleteDC()
memdc.DeleteDC()
win32gui.ReleaseDC(hwin, hwindc)
win32gui.DeleteObject(bmp.GetHandle())
img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
cv2.imwrite('D:/333.jpg',img)
第三种:pyautogui
import pyautogui
import cv2
img = pyautogui.screenshot(region=None) # x,y,w,h
# img.save('screenshot.png')
img = cv2.cvtColor(np.asarray(img),cv2.COLOR_RGB2BGR)
cv2.imwrite('D:/222.jpg',img)
第四种:pyqt5
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
if screen is None:
print('screen is None')
exit(0)
originalPixmap = screen.grabWindow(QtWidgets.QApplication.desktop().winId())
originalPixmap.save('D:/444.jpg', 'png')
以上是关于[python][原创]全屏截图的4种方法的主要内容,如果未能解决你的问题,请参考以下文章
使用Python+Selenium的截图方法,这些必须要知道