python定时截屏实现

作者:天空影 时间:2021-01-28 22:04:09 

写在前面

因为暂时还没有想好做什么具体的某个项目来提升对python的理解,所以就自己想着做一下小玩意来加强一下自己对一些库和方法的理解

分析

1、截屏

截屏的话有pillow这个模块,并且就两条语句,后面会看到

2、定时任务

定时的话涉及到时间,所以会引入time这个模块

3、保存

保存的话因为是会有多个图片,所以需要一个目录来接,这里我就放在了根目录下的png目录下,并且对目录是否存在做了处理

代码


# python3 author jin xiang
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 Screenshot():
nowtime = time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime(time.time()))
print(nowtime)
# 截屏语句很简单的
 im = ImageGrab.grab()
 # 保存(图个有png路径或者别的路径需要在这个路径下有这个目录,不然报错,所以我前面是做了规避,没路径我就自己建一个)
 im.save(r'png\%s.png' %(nowtime))
while True:  
print("截图!")
 Screenshot()

print("暂停")
 print("\n")
 time.sleep(10) #定时10s看一下

效果

python定时截屏实现

python定时截屏实现

图片成功保存了。

补充知识:python opencv 定时器 摄像头自动截图小程序

我就废话不多说了,大家还是直接看代码吧~


import threading
import cv2
global timer
import sys

def shot_img():
 global num
 success, frame = cameraCapture.read()
 path = "H://pythonr"
 cv2.imwrite( path +'a[num]' + '.jpg', frame)
 print(num)
 num += 1
 if num==10:
   cameraCapture.release()
   cv2.destroyAllWindows()
   sys.exit()
 timer = threading.Timer(1, shot_img)
 timer.start()

if __name__ == '__main__':
 num=0
 cameraCapture = cv2.VideoCapture(0)
 timer = threading.Timer(1,shot_img)
 timer.start()

来源:https://blog.csdn.net/jx950915/article/details/90676298

标签:python,截屏
0
投稿

猜你喜欢

  • Python学习pygal绘制线图代码分享

    2022-12-02 10:51:48
  • 一个jquery日期选取插件源码

    2009-12-23 19:15:00
  • php+html5基于websocket实现聊天室的方法

    2023-11-15 06:58:58
  • 基于telepath库实现Python和JavaScript之间交换数据

    2023-05-24 04:20:39
  • Python利用Pillow(PIL)库实现验证码图片的全过程

    2022-05-18 21:27:47
  • Python实现简单猜数字游戏

    2021-03-22 14:31:50
  • 将各种类型或对象都转变为数组

    2009-12-28 13:19:00
  • opencv-python图像增强解读

    2022-10-10 04:29:16
  • python Matplotlib模块的使用

    2022-12-26 21:18:37
  • SQL语句 一个简单的字符串分割函数

    2011-10-24 20:06:33
  • PHP开发中常用的三个表单验证函数使用小结

    2023-11-21 19:15:50
  • python 获取计算机的网卡信息

    2023-08-11 21:12:06
  • Python 元组(Tuple)操作详解

    2023-06-28 15:36:39
  • Mootools 1.2教程(1)——MooTools介绍

    2008-11-12 13:01:00
  • Firebug 必须掌握的技巧

    2009-12-21 20:04:00
  • 基于python图书馆管理系统设计实例详解

    2023-06-28 23:44:13
  • 详解Python的Flask框架中生成SECRET_KEY密钥的方法

    2023-03-07 13:46:07
  • Django学习笔记之为Model添加Action

    2021-04-09 16:32:26
  • 解析smarty模板中类似for的功能实现

    2023-11-15 12:53:40
  • asp 过滤非法字符函数

    2011-04-07 10:39:00
  • asp之家 网络编程 m.aspxhome.com