pu369com

python-mss 截图简单示例

python-mss 是一个速度非常快的截图和录像工具,支持跨平台,使用纯 python 语言开发。

1
pip install mss

 截取指定区域并保存为png

1
2
3
4
5
6
7
8
#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")
    

  获取一个或多个像素点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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()    
    

 监控某区域出现像素点

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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

posted on   pu369com  阅读(1340)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
历史上的今天:
2019-03-15 golang 日期时间处理
2019-03-15 golang json 读写配置文件
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示