使用pillow库
判断存储图片的文件夹是否存在,若不存在,则创建该文件夹; 以年月日创建文件夹,将每天的图片存储在该文件夹中; 调用pillow库定时截图
import time
from PIL import ImageGrab
import os
absPath = os. path. abspath( '.' )
path = [ x for x in os. listdir( '.' ) if os. path. isdir( x) ]
if 'mapPictures' in path:
pass
else :
pngPath = os. path. join( absPath, 'mapPictures' )
os. mkdir( pngPath)
def create_folder ( ) :
"""
每一天建立一个文件夹
:return:
"""
now_time = time. strftime( '%Y_%m_%d' , time. localtime( time. time( ) ) )
absPath = os. path. abspath( './mapPictures/%s' % now_time)
dirs1 = [ ]
for root, dirs, files in os. walk( './mapPictures' ) :
for dir in dirs:
dirs1. append( dir )
if now_time in dirs1:
pass
else :
pngPath = os. path. join( absPath)
os. mkdir( pngPath)
def screen_shot ( ) :
"""
截屏
"""
dir = time. strftime( '%Y_%m_%d' , time. localtime( time. time( ) ) )
now_time = time. strftime( '%Y_%m_%d_%H_%M_%S' , time. localtime( time. time( ) ) )
print ( now_time)
im = ImageGrab. grab( )
im. save( r'./mapPictures/%s/%s.png' % ( dir , now_time) )
if __name__ == '__main__' :
while True :
print ( "截图!" )
create_folder( )
screen_shot( )
print ( "暂停\n" )
time. sleep( 300 )