我正在编写一个python脚本,每30秒截图一次,并将输出图像与实际日期一起保存。
问题是脚本使用time.ctime()
输出的每个字母保存每个图像(Tue Jan 3 01:30:53 2017
):
T.png
u.png
e.png
...
如何保存每个图像输出与实际完成日期?
import pyscreenshot as ImageGrab
import time
max_prints = 10
counter = 0
timer = time.ctime()
while counter < max_prints:
for mark in timer:
im=ImageGrab.grab()
#im.show()
im=ImageGrab.grab(bbox=(10,10,500,500))
#im.show()
ImageGrab.grab_to_file(str(mark) + ".png")
time.sleep(30)
此循环可能会有所帮助:
while counter < max_prints:
im = ImageGrab.grab(bbox=(10, 10, 500, 500))
filename = str(dt.datetime.now()).replace(' ', 'T').split('.')[0] + ".png"
ImageGrab.grab_to_file(filename)
time.sleep(5)
你能试试这样的东西吗?
import pyscreenshot as ImageGrab
import time
max_prints = 10
counter = 0
while counter < max_prints:
im=ImageGrab.grab()
#im.show()
im=ImageGrab.grab(bbox=(10,10,500,500))
#im.show()
timer = time.ctime()
ImageGrab.grab_to_file(timer + ".png")
time.sleep(30)
counter += 1
如果您愿意,可以通过更改以下行来拥有以后更容易处理的文件名:
timer = time.ctime()
ImageGrab.grab_to_file(timer + ".png")
对此:
timer = time.ctime()
timestamp = timer.replace(' ', '_')
timestamp = timestamp.replace(':', '_')
filename = "{}.png".format(timestamp)
ImageGrab.grab_to_file(filename)
但这取决于您希望如何命名文件。