How to convert JPG to WEBP using Python

How to Convert JPG to WEBP Using Python?

Converting image formats is a common task in web development and digital content management. The WEBP format, developed by Google, offers superior compression and quality compared to JPG, making it ideal for web performance. In this guide, we'll explore how to convert JPG images to WEBP using Python—a simple and efficient method.

Convert JPG to Webp offline


Why Convert JPG to WEBP?

WEBP provides:

  • Better compression – Smaller file sizes without significant quality loss.
  • Transparency support – Unlike JPG, WEBP supports alpha channels.
  • Faster loading – Improves website performance and SEO rankings.
WebP file format



Prerequisites

Before proceeding, ensure you have:

  • Python installed (version 3.6 or higher).
  • The Pillow library (a fork of PIL, Python Imaging Library).

Installing Pillow

Install Pillow using pip:

pip install Pillow

Step-by-Step Conversion Process

1. Import the Required Module

Start by importing the Image module from Pillow:

from PIL import Image

2. Load the JPG Image

Use Image.open() to load the input JPG file:

image = Image.open("input.jpg")

3. Convert and Save as WEBP

Save the image in WEBP format using image.save():

image.save("output.webp", "WEBP")

4. Optional: Adjust Quality (Optional)

WEBP supports a quality parameter (0-100). Higher values mean better quality but larger files:

image.save("output.webp", "WEBP", quality=90)

Complete Python Script

Here’s the full script to convert JPG to WEBP:

from PIL import Image

def convert_jpg_to_webp(input_path, output_path, quality=90):
    try:
        image = Image.open(input_path)
        image.save(output_path, "WEBP", quality=quality)
        print(f"Successfully converted {input_path} to {output_path}")
    except Exception as e:
        print(f"Error: {e}")

# Example usage
convert_jpg_to_webp("input.jpg", "output.webp")

Handling Multiple Files

To convert multiple JPG files in a folder, use os.listdir():

import os
from PIL import Image

input_folder = "jpg_images"
output_folder = "webp_images"

os.makedirs(output_folder, exist_ok=True)

for file in os.listdir(input_folder):
    if file.endswith(".jpg"):
        input_path = os.path.join(input_folder, file)
        output_path = os.path.join(output_folder, file.replace(".jpg", ".webp"))
        convert_jpg_to_webp(input_path, output_path)

Summary: Converting JPG to WEBP in Python is straightforward using the Pillow library. This method ensures smaller file sizes, better performance, and maintains image quality. Ideal for web developers and content creators.

Incoming search terms
- How to convert JPG to WEBP using Python
- Best Python library for image conversion
- Convert multiple JPG to WEBP in Python
- Python script for JPG to WEBP conversion
- Optimize images with WEBP in Python
- How to reduce image size using WEBP
- Pillow library for image conversion tutorial
- Save JPG as WEBP with Python code
- Batch convert images to WEBP format
- Python image processing for WEBP files

No comments:

Post a Comment