Image Optimization

Published on
2 mins read

Efficiently Convert Image Formats: How to Optimize Your Images with cwebp

Optimizing images on a website is a crucial step in improving website performance. By converting images to a more efficient format such as WebP, you can significantly reduce the file size of images without sacrificing quality. This can result in faster page load times, improved user experience, and even better search engine rankings.

Steps

  1. Install the Google WebP package if it is not already installed on your system. You can do this by running the following command in the terminal:
$sudo apt-get install webp # if package isn't found run sudo apt-get update first
  1. Navigate to the directory where the image files are located using the "cd" command in the terminal. For example, if the images are in the "Pictures" folder in your home directory, you can type:
  cd ~/Pictures
  1. Run the following command to convert all the ".png" and ".jpg" files to "*.webp" format:
for file in *.png *.jpg; do cwebp -q 80 "$file" -o "${file%.*}.webp"; done

This command uses a for loop to iterate over all the .png and .jpg files in the current directory, and converts each file to .webp format using the cwebp tool. The -q 80 option sets the quality of the output image to 80 (you can adjust this value as needed), and the ${file%.}.webp expression constructs the output filename by removing the extension of the input file and appending .webp.