You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何用PyAutoGUI实现定时截图并按序列命名保存多文件?

Fix: Save Screenshots as Sequential Files (image(1), image(2), etc.)

Hey there! The problem here is straightforward—you’re overwriting the same image.png file every time because your filename is hardcoded. To save unique sequential screenshots, we need to generate a dynamic filename with an incrementing number each time. Here are two reliable solutions:

Approach 1: Use a Simple Counter (Great for Continuous Runs)

This method uses a global counter that increments each time a screenshot is taken. It’s simple and works perfectly if you don’t need to resume numbering after restarting the program.

import pyautogui
import threading

# Start counting from 1
screenshot_count = 1

def ScreenShotTimer():
    global screenshot_count
    # Schedule the next screenshot to run in 5 seconds
    threading.Timer(5.0, ScreenShotTimer).start()
    
    # Generate the sequential filename
    save_path = rf'C:\Users\censored\Desktop\screenshot\imgs\image({screenshot_count}).png'
    myScreenshot = pyautogui.screenshot()
    myScreenshot.save(save_path)
    
    print(f'Saved: {save_path} | Program Is Still Running.')
    
    # Increase counter for the next screenshot
    screenshot_count += 1

ScreenShotTimer()

Approach 2: Auto-Detect Existing Files (Robust for Restarts)

If you want the program to pick up where it left off even after restarting, this method scans the target directory to find the highest existing screenshot number, then uses the next number in sequence. We’ll use os for directory handling and re to parse filenames.

import pyautogui
import threading
import os
import re

def get_next_screenshot_number():
    target_dir = r'C:\Users\censored\Desktop\screenshot\imgs'
    # Create the directory if it doesn't exist yet
    os.makedirs(target_dir, exist_ok=True)
    
    # Regex to match filenames like "image(1).png" and extract the number
    filename_pattern = re.compile(r'image\((\d+)\)\.png')
    highest_num = 0
    
    # Check every file in the directory
    for file in os.listdir(target_dir):
        match = filename_pattern.match(file)
        if match:
            current_num = int(match.group(1))
            if current_num > highest_num:
                highest_num = current_num
    
    # Return the next number to use
    return highest_num + 1

# Initialize counter with the next available number
screenshot_count = get_next_screenshot_number()

def ScreenShotTimer():
    global screenshot_count
    threading.Timer(5.0, ScreenShotTimer).start()
    
    save_path = rf'C:\Users\censored\Desktop\screenshot\imgs\image({screenshot_count}).png'
    myScreenshot = pyautogui.screenshot()
    myScreenshot.save(save_path)
    
    print(f'Saved: {save_path} | Program Is Still Running.')
    screenshot_count += 1

ScreenShotTimer()

Key Notes:

  • Approach 1 is lightweight and easy to implement, but resets to 1 if you restart the program.
  • Approach 2 is more robust—even if you stop and restart the script, it won’t overwrite existing files and will continue the sequence correctly.

内容的提问来源于stack exchange,提问作者user12741608

火山引擎 最新活动