Change non latin characters letters to latin characters

The script will process all .webp images in the specified directory, remove accents from filenames, and handle potential naming collisions as discussed earlier.

Change non latin characters letters to latin characters.


be sure to install “pip install Pillow unidecode


import os
from unidecode import unidecode
from PIL import Image

def remove_accents(input_str):
    return unidecode(input_str)

def rename_with_check(original_path, new_name):
    base, extension = os.path.splitext(new_name)
    counter = 1
    new_path = os.path.join(os.path.dirname(original_path), new_name)

    # Check for files that might have the same name considering case insensitivity
    while os.path.exists(new_path) or any(fname.lower() == new_name.lower() for fname in os.listdir(os.path.dirname(original_path))):
        new_name = f"{base}_{counter}{extension}"
        new_path = os.path.join(os.path.dirname(original_path), new_name)
        counter += 1

    os.rename(original_path, new_path)
    return new_name

def process_images(directory):
    # Check each file in the given directory
    for filename in os.listdir(directory):
        if filename.endswith('.webp'):
            # Construct the full file path
            filepath = os.path.join(directory, filename)

            # Open and immediately save the image to ensure it's a valid image
            with as img:

            # Remove accents from the filename
            new_name = remove_accents(filename)

            # Rename the file with a check for existing names
            final_name = rename_with_check(filepath, new_name)
            print(f"Processed {filename} -> {final_name}")

if __name__ == "__main__":
    directory = input("Enter the directory containing the webp images: ")


Leave the first comment