How to Convert PNG & JPG Images to WebP using ChatGPT
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](/widgets/bulk-webp-converter/) 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.
We highly recommend using WebP for all your images. It will speed up your site and lead to better search result [rankings](/widgets/listicles-finder/)
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](/blog/python-hacks-marketers/) 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
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 versionsGood 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? ```
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. ```python 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.
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% |
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.