Back

The Ultimate Directory Image Conversion Tool Made with Python

Convert all images in the program's directory and its subdirectories without extra installation.

Featured image of post The Ultimate Directory Image Conversion Tool Made with Python

Table of contents

# The Ultimate Directory Image Conversion Tool Made with Python

TL;DR / Geek Summary:

  • Python-powered recursive image mass-converter utilizing the Pillow and tqdm libraries.
  • Supports bidirectional logic: WebP-to-JPG and multi-format-to-WebP (compression-focused).
  • Zero-install standalone deployment optimized for legacy Windows environments (Win7+).

# Introduction

This program is designed to convert all images within its directory, including all subdirectories. It is compatible with Windows 7 and above.

# Practical Effect

Effect

# Source Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import sys
import time
from PIL import Image
from tqdm import tqdm


def convert_to_webp(input_file, output_file, quality=80):
    try:
        with Image.open(input_file) as im:
            im.save(output_file, "webp", quality=quality)
        print(f"Converted: {input_file} => {output_file}")
    except Exception as e:
        print(f"Error converting file: {input_file}")
        print(str(e))


def convert_to_jpg(input_file, output_file):
    try:
        with Image.open(input_file) as im:
            im.save(output_file, "JPEG")
        print(f"Converted: {input_file} => {output_file}")
    except Exception as e:
        print(f"Error converting file: {input_file}")
        print(str(e))


def process_folder(folder_path, conversion_type):
    for root, dirs, files in os.walk(folder_path):
        for filename in tqdm(files):
            if conversion_type == 1 and filename.lower().endswith('.webp'):
                input_file = os.path.join(root, filename)
                output_file = os.path.splitext(input_file)[0] + ".jpg"
                convert_to_jpg(input_file, output_file)
            elif conversion_type == 2 and any(
                    filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.webp', '.jpeg']):
                input_file = os.path.join(root, filename)
                output_file = os.path.splitext(input_file)[0] + ".webp"
                convert_to_webp(input_file, output_file)


if __name__ == "__main__":
    print("LEl_FENG xpdbk.com Batch Image Processor. Continue? [Press Enter]")
    input()
    print("Convert WebP to JPG [1]\nConvert PNG and JPG to WebP [2]\nPlease enter 1 or 2:")
    conversion_type = int(input())
    print("WARNING: This program will convert ALL images in the program's folder. Please use with caution. [Press Enter to continue]")
    input()
    folder_path = os.path.dirname(sys.argv[0])
    process_folder(folder_path, conversion_type)
    print("Conversion complete. The program will exit in 5 seconds.")
    time.sleep(5)