How to Convert PNG & JPG Images to WebP using ChatGPT

03 Oct 2024
How to use ChatGPT to convert png, jpg, or jpeg files to webp.

WebP files aren’t new, but they’re still not widely adopted (though some CMS like Wix automatically convert all images to WebP before deployment). This article explains what WebP images are and how you can use ChatGPT to create them quickly.

What are WebP files, and why are they good for SEO?

WebP is an image format developed by Google. This file type has better compression than jpg and png files, meaning the image files are smaller (and load faster) without losing quality. Since Google favors fast-loading sites in search results, converting your images to WebP can help with SEO.

Table of how webp differs from PNG and JPG

Not using WebP isn’t the end of the world, but these small improvements in page speed can add up. Plus, Google’s Page Speed Insights will always ding you for any image files that aren’t WebP.

We highly recommend using WebP for all your images. It will speed up your site and lead to better search result rankings

But WebP files can be a pain. Unlike png and jpeg/jpg files, there’s no easy way to create them. You can’t just change the file name, and you can’t convert them using Preview / Adobe products.

How do I convert WebP files?

It’s possible to convert png/jpg files online using a tool like Cloud Convert. And for one-off files, go for it! I’m not going to sit here and tell you self-hosting a Python is the simplest method.

BUT! As a marketer myself, I found these solutions limited. Mainly:

  1. Many bombard you with ads
  2. Most throttle your usage. For some blog articles, I had 15-20 images to convert. I either had to use multiple tools to do all 20 or upload them slowly so I didn’t hit the conversion cap
  3. The conversion process is slow. You have to transfer the files, wait for each one to convert, then download them
online webp converter

SO! I created a Python script that I run locally, which:

  1. Takes any number of image files
  2. Converts them to WebP in milliseconds
  3. Does this all for free without ads!

What before may have taken 10 minutes now takes 10 seconds.

The rest of this article outlines how you can use the same script yourself to convert image files to WebP.

How can I use ChatGPT to create WebP files?

You can easily convert png/jpg files to WebP using the below script, which was built by ChatGPT. And you can use ChatGPT as your troubleshooter and guide if you run into any issues.

How do I run this WebP script?

To convert png/jpg images to WebP, just follow the below instructions. You’ll be setting up a script on your computer that will:

  1. Take a folder of png/jpg/jpeg files
  2. Produce a new folder with WebP versions

Good news here - no coding experience needed! You just need to follow a few steps.

Set up your dependencies

This just means you need some basic programs downloaded onto your computer, specifically:

  • Python, version 3 or higher
  • Pillow, which is a Python tool for imaging

How you download these will depend on your OS (I use MacOS). You’ll also need to find your system’s command-line interface (CLI). On Mac this is the Terminal program. It’s “Command Prompt” or “cmd.exe” on Windows.

To download these, give this prompt to ChatGPT.

I have a Python3 script for converting .webp files to png files. It uses Pillow. I use [insert your OS] operating system. Can you please provide instructions for the dependences I need to run it, giving me CLI prompts?
chatgpt prompt for web

Then follow its instructions. You’ll need to find and use your CLI tool.

To troubleshoot errors, copy & paste any CLI errors into ChatGPT. It'll guide you through issues.

Create the folder the script will live in

  • First, download Sublime Text (unless you already have a good text editor)

  • Create a folder called webp in your documents folder.

  • Create a folder within the webp folder called images.

  • Create an empty Python file called webp.py in the webp folder. To do this:

  1. Open SublimeText, create a new file, and save it in the webp folder as webp.py
  2. Or use your CLI. For instance, with MacOS Terminal, you can follow these instructions (each line is a new prompt). The touch command creates a new file with that name.
cd documents/webp
touch webp.py

Create the script

  • Open that webp.py file with SublimeText.

  • Copy the below code into the file and save.

import os
from pathlib import Path
from PIL import Image

input_folder = 'images'
output_folder = 'compressed_images'
quality = 60

# Use pathlib for easier path handling
input_path = Path(input_folder)
output_path = Path(output_folder)

# Create output folder if it doesn't exist
output_path.mkdir(parents=True, exist_ok=True)

# Helper function to determine if a file is an image
def is_image(file):
    return file.suffix.lower() in ['.png', '.jpeg', '.jpg']

# Iterate through files in the input folder
for file in input_path.iterdir():
    if file.is_file() and is_image(file):
        try:
            img = Image.open(file).convert('RGBA')

            webp_filename = file.stem + '.webp'
            img.save(output_path / webp_filename, 'webp', quality=quality, lossless=False)

        except Exception as e:
            print(f"Error processing file '{file}': {e}")

print('Done!')

Run the script

  • Put all image files (png, jpeg, jpg) you want to convert into the images folder within your webp folder.
Chatgpt webp script folder
  • Ask ChatGPT how to run a Python script.
I have file webp.py in path /documents/webp. I use [X] operating system. I have python3 downloaded. How can I run the prompt through my CLI?
Chatgpt webp prompt

Then follow these instructions. For instance, on MacOS, you’d follow these prompts (each line is a different prompt), which you would put into Terminal.

cd documents/webp
python3 webp.py
  • Once run, the CLI should say, “done”. It will create a folder called compressed_images which will have all the converted images.
Chatgpt folder of compressed images

Some notes:

  • It will automatically create a compressed_images folder. You do not need to add it or delete this after the first run
  • Ensure the width/height of your png/jpg images are commensurate to their size on the page. For instance, if you have a logo that is taking up a 200x50 size spot, you don’t want to convert a 2000x500px image. So, before running the script, reduce the image size
  • Most image tools can’t edit WebP files, so make sure the file you are converting is correctly cropped/resized/etc.
  • If you would like to increase the quality of the image (or decrease it), change the quality number below in the webp.py file. A higher number will be a bigger file, but higher quality. The typical quality range is 50-80
input_folder = 'images'
output_folder = 'compressed_images'
quality = 60

How well does the ChatGPT WebP script compress?

Here’s actual images I compressed using the script.

PNG Size WebP Size % Change
449kb 45kb -90%
401kb 37kb -90%
303kb 61kb -80%

When I ran it through an additional external compressor, you can see that it was as optimized as possible!

Webp files are totally compressed by chatgpt

There you go. Using ChatGPT and the Python script above, you can convert .png, .jpeg, and .jpg files into .webp files in just a few seconds.

Get updates about new widgets

We don't sell anything, so you'll never receive a sales email from some random BDR asking to chat. In fact, you'll probably never receive a single email from us, as we have yet to connect a form tool and have no idea what happens to emails you submit.