Home

Converting Images to WebP

WebP is a modern image format that provides superior lossless and lossy compression for images on the web. It was developed by Google, and it is widely supported by web browsers and other tools.

One way to convert image files to WebP is to use the cwebp command-line tool. This tool is part of the WebP package, and it can be used to convert a variety of image file types (such as JPG, PNG, and GIF) to WebP.

Here is an example of how to use the cwebp command to convert a JPG file to WebP:

cwebp input.jpg -o output.webp
cwebp input.jpg -o output.webp

This command will convert the file input.jpg to output.webp, using the default options. The -o flag specifies the output file.

You can also specify various options to control the quality and behavior of the conversion. For example, you can use the -q flag to set the quality of the image, with a value between 0 and 100. A higher value means better quality but larger file size, while a lower value means lower quality but smaller file size.

cwebp input.jpg -q 80 -o output.web

This command will convert the file input.jpg to output.webp with a quality of 80, which should produce good quality images while still reducing the file size.

cwebp however does not handle bmp and gif so if we need to convert them into webp then we will need to convert them first into something like png

To handle my needs I made this script for converting images to webp

#!/bin/bash

# Find all image files in the current directory and its subdirectories
find . -type f \( -iname \*.jpg -o -iname \*.jpeg -o -iname \*.png -o -iname \*.bmp -o -iname \*.gif \) -print0 |

# Convert the images to webp, using all available hardware resources
while read -d $'\0' file; do
    # Determine the file extension
    ext="${file##*.}"

    # Use the appropriate command for the file extension
    if [ "$ext" == "gif" ]; then
        # Convert gif to png
        convert "$file" "${file%.*}.png"
        # Convert png to webp
        cwebp -quiet "${file%.*}.png" -lossless -o "${file%.*}.webp"
        # Delete the png file
        rm "${file%.*}.png"
    elif [ "$ext" == "bmp" ]; then
        # Convert bmp to png
        convert "$file" "${file%.*}.png"
        # Convert png to webp
        cwebp -quiet "${file%.*}.png" -q 80 -o "${file%.*}.webp"
        # Delete the png file
        rm "${file%.*}.png"
    else
        # Convert jpeg, png, or other format to webp
        cwebp -quiet "$file" -q 100 -z 9 -mt -alpha_q 100 -o "${file%.*}.webp"
    fi

    # Delete the original file
    rm "$file"

    echo "Converted and deleted $file"
done

echo "Conversion complete!"
#!/bin/bash

# Find all image files in the current directory and its subdirectories
find . -type f \( -iname \*.jpg -o -iname \*.jpeg -o -iname \*.png -o -iname \*.bmp -o -iname \*.gif \) -print0 |

# Convert the images to webp, using all available hardware resources
while read -d $'\0' file; do
    # Determine the file extension
    ext="${file##*.}"

    # Use the appropriate command for the file extension
    if [ "$ext" == "gif" ]; then
        # Convert gif to png
        convert "$file" "${file%.*}.png"
        # Convert png to webp
        cwebp -quiet "${file%.*}.png" -lossless -o "${file%.*}.webp"
        # Delete the png file
        rm "${file%.*}.png"
    elif [ "$ext" == "bmp" ]; then
        # Convert bmp to png
        convert "$file" "${file%.*}.png"
        # Convert png to webp
        cwebp -quiet "${file%.*}.png" -q 80 -o "${file%.*}.webp"
        # Delete the png file
        rm "${file%.*}.png"
    else
        # Convert jpeg, png, or other format to webp
        cwebp -quiet "$file" -q 100 -z 9 -mt -alpha_q 100 -o "${file%.*}.webp"
    fi

    # Delete the original file
    rm "$file"

    echo "Converted and deleted $file"
done

echo "Conversion complete!"

This script does the following:

  1. Uses the find command to search for all image files (with extensions .jpg, .jpeg, .png, .bmp, and .gif) in the current directory and its subdirectories.
  2. Reads the list of image files one by one, and converts each file to webp using the cwebp command.
  3. Determines the file extension of each file using parameter expansion.
  4. Uses an if statement to choose the appropriate conversion process for the file extension. For gif files, the convert command is used to convert the gif to a png file, and then the cwebp command is used to convert the png to a webp file. For bmp files, the same process is used. For other file types, the cwebp command is used directly to convert the file to webp.
  5. Deletes the original image file after it has been converted to webp.
  6. Prints a message indicating that the file has been converted. When all image files have been processed, the script prints a final message indicating that the conversion is complete.

This script is designed to be as efficient as possible, using all available hardware resources to accelerate the conversion process. It is also designed to be easy to use and modify, with clear and concise code.

I hope this script and this blog post are helpful for anyone who wants to convert their image files to WebP! Let me know if you have any questions or suggestions.