Python 使用 pyautogui 保存多张图片

Python save multiple images with pyautogui

我正在制作一个截图程序,每 5 秒截图一次。但它只会保存 1 个 .png 文件。因为每次的名字都是一样的,不会重复。

如何将它们保存为图像(1)、图像(2)、图像(3)....

这是我的代码:

import pyautogui
import threading

#myScreenshot = pyautogui.screenshot()
#myScreenshot.save(r'C:\Users\censored\Desktop\screenshot\imgs\image.png')


def ScreenShotTimer():
    threading.Timer(5.0, ScreenShotTimer).start()
    myScreenshot = pyautogui.screenshot()
    myScreenshot.save(r'C:\Users\censored\Desktop\screenshot\imgs\image.png')
    print('Program Is Still Running.')

ScreenShotTimer()

谢谢你的帮助!

import pyautogui

# Solution 1

import time

for i in range(60):
    ss = pyautogui.screenshot()

    ss.save(f"SS {i}.png")

    time.sleep(5)


# Solution 2

import threading
import functools as ft

def screenshot(index: int = 0):
    ss = pyautogui.screenshot()

    ss.save(f"SS {index}.png")

    threading.Timer(5.0, ft.partial(screenshot, index+1)).start()

screenshot()

两种解决方案。

  1. 使用 for 循环(很容易成为 while 循环)。
  2. 使用 threading.Timerfunctools.partial 调用 使用不同的参数再次运行。