网络编程
位置:首页>> 网络编程>> Python编程>> python定时截屏实现

python定时截屏实现

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

标签:python,截屏

写在前面

因为暂时还没有想好做什么具体的某个项目来提升对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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com