Python图片批量重命名序列异常求助:需生成a-z/aa-bb…格式
问题解决:修改文件名后缀生成逻辑
问题出在next_name函数的实现逻辑上,原代码生成的是类似Excel列名的序列(a,b,...z,aa,ab,...az),而你需要的是每个阶段重复相同字符的序列(a-z, aa-bb-zz, aaa-bbb-zzz...)。
修改后的核心函数
替换原代码中的next_name函数为以下实现:
def next_name(index): # 计算字符重复次数:每26个文件进入下一个长度阶段 repeat_count = (index // 26) + 1 # 循环获取a-z对应的字符 char = chr(97 + index % 26) # 返回重复指定次数的字符 return char * repeat_count
完整修改后代码
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): # 计算字符重复次数:每26个文件进入下一个长度阶段 repeat_count = (index // 26) + 1 # 循环获取a-z对应的字符 char = chr(97 + index % 26) # 返回重复指定次数的字符 return char * repeat_count 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()
逻辑说明
repeat_count = (index // 26) + 1:每26个文件为一组,第一组(0-25)字符重复1次,第二组(26-51)重复2次,第三组(52-77)重复3次,以此类推。char = chr(97 + index % 26):通过取余运算循环获取a-z字符,0对应a,1对应b...25对应z,26取余26仍为0对应a,完美匹配每组的字符需求。char * repeat_count:将字符重复指定次数,生成你需要的后缀格式。
修改后即可生成a、b…z、aa、bb…zz、aaa…bbb…的命名序列。
内容的提问来源于stack exchange,提问作者Logan
相关产品推荐
相关产品推荐

