python-mss 是一个速度非常快的截图和录像工具,支持跨平台,使用纯 python 语言开发。
pip install mss
截取指定区域并保存为png
#encoding=utf-8 import mss with mss.mss() as sct: box ={"top":50,"left":50,"width":200,"height":200} im = sct.grab(box) mss.tools.to_png(im.rgb,im.size,output="1.png")
获取一个或多个像素点
#encoding=utf-8 import mss from PIL import Image with mss.mss() as sct: box ={"top":0,"left":0,"width":200,"height":200} sct_img = sct.grab(box) #pixel = sct_img.pixel(0, 0) #print(pixel) img = Image.new("RGB", sct_img.size) pixels = zip(sct_img.raw[2::4], sct_img.raw[1::4], sct_img.raw[::4]) a = list(pixels) #print(a) img.putdata(a) #img.show()
监控某区域出现像素点
#encoding=utf-8 import mss,time from PIL import Image with mss.mss() as sct: box ={"top":70,"left":220,"width":9,"height":12} while True: time.sleep(0.2)#不希望太快了 sct_img = sct.grab(box) img = Image.new("RGB", sct_img.size) pixels = zip(sct_img.raw[2::4], sct_img.raw[1::4], sct_img.raw[::4]) a = list(pixels) if (192,0,192) in a: print("出现") else: print("消失")
更多详细资料请参考官方链接 https://github.com/BoboTiG/python-mss
参考:https://blog.csdn.net/qq_34160248/article/details/127001100
标签:box,img,示例,python,mss,raw,sct From: https://www.cnblogs.com/pu369/p/17218099.html