The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a batch photo renamer

Here’s a simple yet functional batch photo renamer in Python. It allows you to rename all images in a folder according to a pattern you define, like Vacation_001.jpg, Vacation_002.jpg, and so on.


🔧 Features:

  • Works with .jpg, .jpeg, .png, .gif, and .bmp by default

  • Allows customizable base name

  • Numbers files sequentially with optional padding

  • Skips non-image files


🐍 Python Script: Batch Photo Renamer

python
import os def batch_rename_photos( folder_path, base_name="Image", start_index=1, padding=3, extensions=(".jpg", ".jpeg", ".png", ".gif", ".bmp") ): if not os.path.isdir(folder_path): print(f"Error: '{folder_path}' is not a valid directory.") return photos = [f for f in os.listdir(folder_path) if f.lower().endswith(extensions)] photos.sort() # Ensures consistent order if not photos: print("No image files found in the specified directory.") return print(f"Renaming {len(photos)} files...") for i, filename in enumerate(photos, start=start_index): ext = os.path.splitext(filename)[1] new_name = f"{base_name}_{str(i).zfill(padding)}{ext}" src = os.path.join(folder_path, filename) dst = os.path.join(folder_path, new_name) try: os.rename(src, dst) print(f"Renamed: {filename}{new_name}") except Exception as e: print(f"Failed to rename {filename}: {e}") print("Batch renaming completed.") # Example usage: if __name__ == "__main__": folder = input("Enter the path to the folder: ") base = input("Enter base name for files (default: Image): ") or "Image" try: start = int(input("Enter starting index (default: 1): ") or "1") pad = int(input("Enter zero padding length (default: 3): ") or "3") except ValueError: print("Invalid input for index or padding. Using defaults.") start, pad = 1, 3 batch_rename_photos(folder, base, start, pad)

🧪 How to Use:

  1. Save this code as batch_renamer.py.

  2. Run it with Python:

    bash
    python batch_renamer.py
  3. Enter the directory containing the images and follow the prompts.


Would you like a GUI version using tkinter, or a command-line only tool with extra options (e.g., dry run, undo, regex matching)?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About