
Resize and Convert Images to WEBP with Python
Are you a website owner or content creator looking to optimize your images for faster loading times without sacrificing quality? This Python-based Image Resizer and Converter Tool is your solution. By resizing images and converting them to the efficient WEBP format, this script is ideal for anyone wanting to improve their website’s SEO, performance, and user experience.
Key Features of the Tool:
- Batch Processing: Automatically process all
.jpgand.jpegfiles in a folder, including subfolders. - WEBP Conversion: Convert images to the lightweight WEBP format for faster web performance.
- Aspect Ratio Preservation: Resize images while maintaining their original proportions.
- Customizable Output: Define maximum dimensions and choose an output folder for organized results.
- Error Handling: Logs any issues encountered during processing for easy troubleshooting.
- Completion Logs: Generates a
complete.txtfile to summarize results and errors.
Why Optimize Images?
- Improve Page Speed: Fast-loading images enhance your website’s SEO ranking and provide a better user experience.
- Reduce Storage Usage: The WEBP format reduces file sizes by up to 35% compared to traditional formats like JPEG.
- Boost Mobile Performance: Optimized images improve performance on mobile devices where bandwidth is limited.
How to Use the Tool:
- Install Dependencies:
Ensure you have Python installed and run the following command to install required libraries:pip install pillow tkinter - Run the Script:
Copy the script below and save it asimage_resizer_converter.py. Run it using:python image_resizer_converter.py - Select Folders:
– Choose an input folder containing your.jpgimages.
– Select an output folder to save the resized and converted WEBP files. - Start the Process:
Click the “Start Resizing and Converting” button. The tool will handle everything, leaving a detailed log of the process.
Python Script
import os
from PIL import Image
import tkinter as tk
from tkinter import filedialog, messagebox
def resize_image(image_path, output_path, max_length=2560):
try:
image = Image.open(image_path)
ratio = min(max_length / max(image.size), 1)
new_size = tuple([int(x * ratio) for x in image.size])
if ratio < 1:
resized_image = image.resize(new_size, Image.LANCZOS)
else:
resized_image = image.copy()
output_file = os.path.splitext(output_path)[0] + '.webp'
resized_image.save(output_file, 'WEBP')
print(f"Converted and saved: {output_file}")
except Exception as e:
print(f"Error processing {image_path}: {e}")
return f"{image_path}: {e}"
def resize_images_in_folder(folder_path, max_length=2560, output_folder=None):
errors = []
for root, dirs, files in os.walk(folder_path):
for filename in files:
if filename.lower().endswith(('.jpg', '.jpeg')):
image_path = os.path.join(root, filename)
rel_dir = os.path.relpath(root, folder_path)
output_dir = os.path.join(output_folder, rel_dir) if output_folder else root
if not os.path.exists(output_dir):
os.makedirs(output_dir)
output_path = os.path.join(output_dir, filename)
error = resize_image(image_path, output_path, max_length)
if error:
errors.append(error)
completion_log_path = os.path.join(output_folder if output_folder else folder_path, 'complete.txt')
with open(completion_log_path, 'w') as log_file:
log_file.write("Image resizing and conversion complete.\n")
if errors:
log_file.write("Errors encountered:\n")
for error in errors:
log_file.write(f"{error}\n")
else:
log_file.write("No errors encountered.")
print("Processing complete. See 'complete.txt' for details.")
messagebox.showinfo("Process Complete", "Image resizing and conversion complete. See 'complete.txt' for details.")
def select_input_folder():
folder_selected = filedialog.askdirectory()
input_folder_var.set(folder_selected)
def select_output_folder():
folder_selected = filedialog.askdirectory()
output_folder_var.set(folder_selected)
def start_resizing():
input_folder = input_folder_var.get()
output_folder = output_folder_var.get()
if not input_folder or not output_folder:
messagebox.showwarning("Input Required", "Please select both input and output folders.")
return
resize_images_in_folder(input_folder, 2560, output_folder)
root = tk.Tk()
root.title("Image Resizer and Converter")
input_folder_var = tk.StringVar()
tk.Label(root, text="Input Folder:").grid(row=0, column=0, padx=10, pady=10)
tk.Entry(root, textvariable=input_folder_var, width=50).grid(row=0, column=1, padx=10, pady=10)
tk.Button(root, text="Browse...", command=select_input_folder).grid(row=0, column=2, padx=10, pady=10)
output_folder_var = tk.StringVar()
tk.Label(root, text="Output Folder:").grid(row=1, column=0, padx=10, pady=10)
tk.Entry(root, textvariable=output_folder_var, width=50).grid(row=1, column=1, padx=10, pady=10)
tk.Button(root, text="Browse...", command=select_output_folder).grid(row=1, column=2, padx=10, pady=10)
tk.Button(root, text="Start Resizing and Converting", command=start_resizing).grid(row=2, column=1, pady=20)
root.mainloop()Share This Tool!
Spread the word about this free tool! Help your fellow web developers, photographers, and digital marketers by sharing this content. Together, we can make the web faster and more efficient.

