python实现定时截图的两种截图方式

一、python调用gtk实现屏幕截图

1、开发环境

        操作系统:linux

        开发语言:python 2.7

        需要提前装好的库:gtk、time、os

2、安装gtk模块

sudo apt-get install python-gtk2-dev python-gtk2-tutorial

3、实现代码

# !/usr/bin/python
# coding=UTF-8

import gtk.gdk
import time
import os

# 在没有png目录时,创建一个png目录
absPath = os.path.abspath('.')
path = [x for x in os.listdir('.') if os.path.isdir(x)]
# print(path)
if 'png' in path:
    pass
else:
    # 创建目录png
    pngPath = os.path.join(absPath,'png')
    os.mkdir(pngPath)

# 截图
def screening():
    # 获取当前时间
    nowtime = time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime(time.time()))
    w = gtk.gdk.get_default_root_window()
    # 获取当前屏幕尺寸
    sz = w.get_size()
    print "The size of the window is %d x %d" % sz
    # 获取屏幕像素颜色
    pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
    # 抓取指定区域
    pb = pb.get_from_drawable(w,w.get_colormap(),0,0,0,0,sz[0],sz[1])
    if (pb != None):
        # 抓取的图片保存在png目录下,命名规则:当前时间,属性:png
        pb.save("png/"+r'%s.png' %(nowtime),"png")
        # 将抓取的图片追加记录在screening.txt文件中
        with open("screen_log.txt","a+") as screen_log:
            screen_log.writelines(nowtime)
            screen_log.writelines("\n")
            # 释放资源
            screen_log.close()
        print "Screenshot saved to png."
    else:
        print "Unable to get the screenshot."
while True:
    screening()
    # 获取抓取图片的记录次数
    count=len(open('screen_log.txt','r').readlines())
    # 将抓取图片的统计数写入number.txt文件中
    with open("number.txt","w") as number:
        number.write(str(count))
        # 释放资源
        number.close()
    # 定时10秒
    time.sleep(10)

二、python使用PIL的ImageGrab模块实现屏幕截图

1、开发环境

        操作系统:windows

        开发语言:python 3

        编译工具:pycharm

        库:PIL

2、实现代码

# python3 author linzhongyunwu

import time
from PIL import ImageGrab
import os

# 在没有png目录时,创建一个png目录
absPath = os.path.abspath('.')
path = [x for x in os.listdir('.') if os.path.isdir(x)]
# print(path)
if 'png' in path:
    #print('yes')
    pass
else:
    #print('no')
    #创建目录
    pngPath = os.path.join(absPath,'png')
    os.mkdir(pngPath)

#截屏
def Screening():
    nowtime = time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime(time.time()))
    print(nowtime)
    # 截屏
    im = ImageGrab.grab()
    # 保存
    im.save(r'png\%s.png' %(nowtime))
    with open("screen_log.txt","a+",newline="") as screen_log:
        screen_log.writelines(nowtime)
        screen_log.writelines("\n")
        screen_log.close()
# count=0
while True:
    # print("截图!")
    Screening()
    # print("暂停")
    # print("\n")
    # count += 1
    count=len(open('screen_log.txt','r').readlines())
    with open("number.txt","w",newline="") as number:
        number.write(str(count))
        number.close()
    time.sleep(10) #定时10s看一下

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现Python每天定时执行脚本的功能,常用的方式有两种:通过Windows创建定时任务和使用Jenkins进行定时执行。如果选择通过Windows创建定时任务执行,可以按照以下步骤操作: 1. 打开Windows任务计划程序,在任务计划程序库中创建一个新任务。 2. 在"常规"选项卡中,给任务取一个名称,并确保选择了与你的Python编译器相对应的操作系统架构。 3. 在"触发器"选项卡中,点击"新建"按钮,设置定时执行的时间和频率。 4. 在"操作"选项卡中,点击"新建"按钮,填写程序或脚本的名称,一般是python.exe。然后在"添加参数"文本框中填写你的Python程序的完整路径,例如:D:\software\unittest_demo\test.py。最后在"起始于"文本框中填写Python编译器的目录,例如:D:\software\python\python.exe。 5. 确认所有设置后,保存任务。 另外一种方式是通过Jenkins来定时执行Python脚本。Jenkins是一个开源的持续集成工具,可以用于自动化构建、测试和部署等任务。你可以按照以下步骤操作: 1. 安装Jenkins并启动服务。 2. 在Jenkins中创建一个新的任务。 3. 在任务的配置中,设置触发器来指定定时执行的时间和频率。 4. 在构建步骤中,添加一个"执行Windows批处理命令"或"执行Shell"的步骤,并填写Python程序的执行命令,例如:python D:\software\unittest_demo\test.py。 5. 保存并运行任务,Jenkins将会在指定的时间点自动执行Python脚本。 以上就是实现Python

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林中云雾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值