import os
import math

# Define the directory
directory = 'tmp'
init = 100

# Get the list of files in the directory, sorted alphabetically
files = sorted([f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))])

# Get the total number of files
total_files = len(files)

# If there are no files, exit
if total_files == 0:
    print("No files to rename in the directory.")
else:
    # Calculate the middle index to split the files into two halves
    middle_index = math.ceil(total_files / 2)  # Round up to handle odd numbers of files

    # Rename the first half to P1.png, P2.png, etc.
    for i in range(middle_index):
        old_name = os.path.join(directory, files[i])
        new_name = os.path.join(directory, f'P{i+1 + init}.png')
        os.rename(old_name, new_name)
        print(f"Renamed {old_name} to {new_name}")

    # Rename the second half to S1.png, S2.png, etc.
    for i in range(total_files - middle_index):
        old_name = os.path.join(directory, files[middle_index + i])
        new_name = os.path.join(directory, f'S{i+1 + init}.png')
        os.rename(old_name, new_name)
        print(f"Renamed {old_name} to {new_name}")