前言
上篇文章讲了python mss截屏,这一篇来讲讲各种方式的优劣(以最高画质) 本文测试均在Windows10x64上进行 分辨率2160x1440
介绍
先说,没用pyqt(没用过)和pywin32尽量支持全平台(win,linux,mac)
PIL.ImageGrab(win&mac)
应该大部分人用过或知道
from PIL import ImageGrab,Image
def grab_pil():
im = ImageGrab.grab()
im.save("pil.jpg",quality=95)
6倍清晰
pyautogui(win&linux&mac)
应该也有不少人知道,实际上pyautogui的screenshot是调用pyscreeze的
import pyautogui
def grab_pag():
im = pyautogui.screenshot()
im.save("pag.jpg",quality=95)
返回PIL.Image,6倍清晰
MSS·1 内存中转换PIL
def grab_mss_pil():
with mss.mss() as m:
im = m.grab(m.monitors[0])
img = Image.new("RGB",im.size)
img.frombytes(im.rgb)
img.save("mss-pil.jpg",quality=95)
MSS·2 文件转换PIL
import os
def grab_mss_pil2():
with mss.mss() as m:
im = m.grab(m.monitors[0])
mss.tools.to_png(im.rgb,im.size,6,"temp.png")
img = Image.open("temp.png")
img.save("mss-pil-2.jpg",quality=95)
os.remove("temp.png")
MSS·3 内存转换Pygame
import pygame as pg
def grab_mss_pg():
with mss.mss() as m:
im = m.grab(m.monitors[0])
s = pg.image.fromstring(im.rgb,im.size,"RGB")
pg.image.save(s,"mss-pg.jpg")
对比
t = time.time()
grab_pil()
print("PIL",time.time() - t)
t = time.time()
grab_pag()
print("PAG",time.time() - t)
t = time.time()
grab_mss_pil()
print("MSS1",time.time() - t)
t = time.time()
grab_mss_pil2()
print("MSS2",time.time() - t)
pg.init()
t = time.time()
grab_mss_pg()
print("MSS->PG",time.time() - t)
结果
第一次 PIL 0.1140146255493164 PAG 0.09567046165466309 MSS1 0.1569356918334961 MSS2 0.47719478607177734 MSS->PG 0.5200934410095215
第二次 PIL 0.13492321968078613 PAG 0.10294556617736816 MSS1 0.14389920234680176 MSS2 0.940852165222168 MSS->PG 0.5586795806884766
第三次 PIL 0.10824346542358398 PAG 0.09102106094360352 MSS1 0.15020394325256348 MSS2 0.46080827713012695 MSS->PG 0.46437573432922363
结论
- 当运行东西少时pyautogui快(为什么)
- PIL很稳定在0.1s左右
- 内存MSS比较稳定,1.5s左右
- 文件MSS最慢且不稳定
- MSS转换pg慢,0.5s左右
方式 | 速度/ (s/图) |
---|
PIL | 约0.1s | pyautogui | 0.08到0.12s | MSS PIL | 约1.5s | MSS PIL file | 0.4到0.9s | MSS Pygame | 约0.5s |
神奇的是,mss.mss().shot()方法居然约0.2s,比转PIL还慢!
本文发于CSDN 于2021/12/25 19:24
|