Reputation: 1
import os
import tkinter as tk
from tkinter import filedialog, messagebox, ttk, simpledialog
from PIL import Image
import threading
# Function to resize and rename images
def resize_and_rename(input_paths, output_folder, base_name, progress_bar):
try:
total_files = len(input_paths)
progress_bar['maximum'] = total_files
# Ensure the output directory exists
os.makedirs(output_folder, exist_ok=True)
# Function to update progress bar
def update_progress(count):
progress_bar['value'] = count
root.update_idletasks()
# Function to generate the next name in sequence
def next_name(index):
name = ""
while index >= 0:
name = chr(97 + index % 26) + name
index = (index // 26) - 1
return name
count = 0
for index, input_path in enumerate(input_paths):
# Open the image file
img = Image.open(input_path)
# Resize the image
img = img.resize((2048, 1536))
# Get the file extension
file_ext = os.path.splitext(input_path)[1]
# Generate output file name
output_name = base_name + next_name(index) + file_ext
output_path = os.path.join(output_folder, output_name)
# Save the resized image to the output directory
img.save(output_path)
# Update progress bar
count += 1
update_progress(count)
messagebox.showinfo("Success", "Images resized and renamed successfully!")
except Exception as e:
messagebox.showerror("Error", f"Error resizing and renaming images: {str(e)}")
# Function to handle button click event for selecting files
def select_files():
file_paths = filedialog.askopenfilenames(title="Select Image Files", filetypes=[("Image files", "*.jpg *.jpeg *.png")])
if file_paths:
output_folder = filedialog.askdirectory(title="Select Output Folder")
if output_folder:
# Ask user for base name
base_name = simpledialog.askstring("Base Name", "Enter base name for renaming:")
if base_name:
# Create progress bar
progress_bar = ttk.Progressbar(root, orient='horizontal', mode='determinate')
progress_bar.pack(fill=tk.X, padx=10, pady=10)
# Start a thread to resize and rename images
threading.Thread(target=resize_and_rename, args=(file_paths, output_folder, base_name, progress_bar)).start()
# Create the main tkinter window
root = tk.Tk()
root.title("Image Resizer and Renamer")
# Create and pack button for selecting files
file_button = tk.Button(root, text="Select Files", command=select_files)
file_button.pack(pady=10)
# Start the tkinter main loop
root.mainloop()
Python code is supposed to resize and rename photos a base name + 'a' 'b'...'z' -'aa' 'bb'...'zz' it instead does is 'aa' 'ab' 'ac'...ect. For example the 27th file should be BASENAMEaa and the 28th should be BASENAMEbb
I have tried a few different things but I can never Get it to follow the a, b, c, ..., z, aa, bb, cc, ..., zz, aaa, bbb, ccc, ..., zzz, ... format
Upvotes: 0
Views: 36