← Back to Home

Jpeg to Webp Converter Python Script Tutorial

Jpeg to Webp Converter Python Script Tutorial: This image converter made with python automatically converts jpeg or jpg images into webp.

1- Make sure you have Python installed on your PC

2- Create a Folder named “Converter” and a sub folder named “images”. The script will use images in this folder to conver to webp and save them into an automatically created folder named “converted_webp”.

3- Create a file named “app.py” and paste the following code into it and save as app.py with saving type set to “all files”.

app.py code

import os
from PIL import Image

def convert_to_webp(input_folder, output_folder, quality=80):
    """
    Scans the images folder for JPG/JPEG files and converts them to WebP.
    Saves the results in a separate folder in the main directory.
    """
    
    # Create the output folder in the main directory if it doesn't exist
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
        print(f"[INFO] Created output directory: {output_folder}")

    valid_extensions = (".jpg", ".jpeg")
    count = 0

    print(f"--- Starting WebP Conversion ---")
    print(f"Source: {input_folder}")
    print(f"Destination: {output_folder}\n")
    
    try:
        for filename in os.listdir(input_folder):
            if filename.lower().endswith(valid_extensions):
                # Path logic
                img_path = os.path.join(input_folder, filename)
                clean_name = os.path.splitext(filename)[0]
                target_path = os.path.join(output_folder, f"{clean_name}.webp")
                
                # Conversion Process
                with Image.open(img_path) as img:
                    # quality=80 is excellent for WebP (good balance)
                    # method=6 provides the best compression but is slower
                    img.save(target_path, "WEBP", quality=quality, method=6)
                
                count += 1
                print(f"[SUCCESS] {filename} -> {clean_name}.webp")
        
        if count == 0:
            print("[NOTICE] No JPG or JPEG files found in 'images' folder.")
        else:
            print(f"\n--- Process Finished! {count} images converted to WebP. ---")

    except Exception as e:
        print(f"[ERROR] An unexpected error occurred: {e}")

if __name__ == "__main__":
    # Main directory where app.py is located
    base_dir = os.path.dirname(os.path.abspath(__file__))
    
    # Folder paths (side by side in the main directory)
    images_input_dir = os.path.join(base_dir, "images")
    webp_output_dir = os.path.join(base_dir, "converted_webp")
    
    # Create 'images' folder if it doesn't exist
    if not os.path.exists(images_input_dir):
        os.makedirs(images_input_dir)
        print(f"[SETUP] Created 'images' folder. Please put your JPGs inside: {images_input_dir}")
    else:
        convert_to_webp(images_input_dir, webp_output_dir)

4- Open the Converter folder. Right click and click on Terminal. Type “dir” and tap on “Enter”

5- Then type “pip install pillow” or “python -m pip install pillow”

6- Now you can run your script. Type “python app.py” and tap on enter.