DevDockTools

Batch Convert Images to WebP on Command Line

Learn how to batch convert images to WebP format on the command line, reducing image size by 25-30% and improving page load times by 10-15%.

By DevDockTools Team3 min read
webpimage optimizationcommand linebatch conversionperformance

Introduction to WebP Format

WebP is a modern image format developed by Google, offering superior compression and quality compared to traditional formats like PNG and JPEG. By converting images to WebP, developers can reduce file size by 25-30% on average, resulting in faster page load times and improved user experience.

Batch Conversion Using cwebp

To batch convert images to WebP on the command line, you can use the cwebp tool. Here's an example command:

cwebp -q 80 input.png -o output.webp

This command converts the input.png image to WebP format with a quality setting of 80, saving the output as output.webp. You can adjust the quality setting to balance file size and image quality.

Batch Conversion Script

To convert multiple images at once, you can create a batch script using the following code:

#!/bin/bash

for file in *.png; do
  cwebp -q 80 "$file" -o "${file%.png}.webp"
done

This script loops through all PNG files in the current directory, converting each one to WebP format with a quality setting of 80.

Comparison of Image Formats

The following table compares the file size and quality of different image formats:

| Format | File Size | Quality | | --- | --- | --- | | PNG | 100KB | High | | JPEG | 80KB | Medium | | WebP | 70KB | High | | GIF | 120KB | Low |

As shown in the table, WebP offers the best balance of file size and quality, making it an ideal choice for web development.

Using ffmpeg for Batch Conversion

Alternatively, you can use ffmpeg to batch convert images to WebP. Here's an example command:

ffmpeg -i input.png -c:v libwebp -q:v 80 output.webp

This command converts the input.png image to WebP format with a quality setting of 80, saving the output as output.webp.

ffmpeg Batch Conversion Script

To convert multiple images at once using ffmpeg, you can create a batch script using the following code:

#!/bin/bash

for file in *.png; do
  ffmpeg -i "$file" -c:v libwebp -q:v 80 "${file%.png}.webp"
done

This script loops through all PNG files in the current directory, converting each one to WebP format with a quality setting of 80.

Next Steps

To get started with batch converting images to WebP, try using the png-to-webp tool to convert a single image and see the file size reduction for yourself. Then, use the techniques outlined in this article to batch convert multiple images and improve the performance of your website.

Frequently Asked Questions

What is the average file size reduction when converting images to WebP?
Converting images to WebP can reduce file size by 25-30% on average, resulting in faster page load times. For example, a 100KB PNG image can be reduced to 70-75KB in WebP format. The [png-to-webp](/tools/image-optimization/png-to-webp) tool can help achieve this reduction.
Can I use the command line to convert multiple images to WebP at once?
Yes, you can use the command line to batch convert multiple images to WebP format using tools like cwebp or ffmpeg. The [image-resizer](/tools/image-optimization/image-resizer) tool can also be used to resize images before converting them to WebP.
How do I verify the integrity of my WebP images after conversion?
You can use tools like [jpg-compressor](/tools/image-optimization/jpg-compressor) to verify the integrity of your WebP images and ensure they are properly compressed. Additionally, you can use the [base64-encoder](/tools/encoding/base64-encoder) tool to encode your WebP images for use in CSS or HTML files.