def rename_images(source_folder, destination_folder): # Check if source folder exists if not os.path.exists(source_folder): print(f"Source folder '{source_folder}' does not exist.") return # Create destination folder if it doesn't exist if not os.path.exists(destination_folder): os.makedirs(destination_folder)
# List all files in the source folder files = os.listdir(source_folder)
# Initialize index for renaming index = 1
# Iterate over each file in the source folder for filename in files: # Check if the file is an image (you may want to add more image extensions) if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')): # Form the full path of the source file source_path = os.path.join(source_folder, filename)
# Form the new filename based on index new_filename = f"img{index}{os.path.splitext(filename)[1]}" # Form the full path of the destination file destination_path = os.path.join(destination_folder, new_filename) #print(new_filename) # Rename and move the file to the destination folder shutil.move(source_path, destination_path) print(f"Moved '{filename}' to '{destination_folder}' as '{new_filename}'")
# Increment index index += 1 else: print(f"Ignored '{filename}' as it is not an image file.")