import os
import pandas as pd
# === RENAMING SCRIPT ===
# Change these paths to where you have the images
input_folder = r"C:\Users\sof12\Desktop\ML\dataset_gbif_artichoke"   # Input folder
output_folder = r"C:\Users\sof12\Desktop\ML\dataset\Carciofo" # Output folder
os.makedirs(output_folder, exist_ok=True)

start_index = 1  # Initial counter

old_names = []
new_names = []

# Get list of files and sort them
files = sorted(os.listdir(input_folder))
counter = start_index

for filename in files:
    full_path = os.path.join(input_folder, filename)
    if not os.path.isfile(full_path):
        continue
    
    # Extract file extension
    _, ext = os.path.splitext(filename)
    
    # Create new name
    new_name = f"Carciofo_GBIF_{counter:03d}{ext}"
    
    # Rename by moving file
    new_path = os.path.join(output_folder, new_name)
    os.rename(full_path, new_path)
    
    # Save the data for CSV
    old_names.append(filename)
    new_names.append(new_name)
    
    counter += 1

# Create CSV
df = pd.DataFrame({"Old_Name": old_names, "New_Name": new_names})
csv_path = os.path.join(output_folder, "change_namesAV.csv")
df.to_csv(csv_path, index=False, encoding="utf-8-sig")

print(f"Renamed {len(new_names)} images.")
print(f"CSV file generated at: {csv_path}")
if new_names:
    print(f"Example first change: {old_names[0]} --> {new_names[0]}")