How to Batch Convert Images to WebP in Linux Terminal Using ImageMagick

Modern web performance and SEO best practices heavily emphasise serving next-generation image formats like WebP. WebP images provide superior lossless and lossy compression compared to legacy JPEG and PNG formats, often reducing file payloads by 25% to 35% without perceptible loss of image clarity. Manually opening graphic design software to export dozens or hundreds of images individually is tedious and impractical.

Linux includes robust command-line image manipulation tooling through ImageMagick. Combining ImageMagick’s magick (or legacy convert/mogrify) utility with simple Bash loops or GNU Parallel allows you to compress and convert entire directory trees of images into production-ready WebP files in seconds.

Installing ImageMagick and WebP Libraries on Linux

Ensure your Linux environment has ImageMagick and the underlying Google WebP compression libraries installed.

  1. Launch your terminal and update the package repository cache:
    sudo apt update
    (For Fedora, RHEL, or AlmaLinux: sudo dnf check-update)
  2. Install ImageMagick along with WebP codec support:
    sudo apt install imagemagick webp -y
    (On RHEL/CentOS systems: sudo dnf install ImageMagick ImageMagick-devel libwebp-tools -y)
  3. Verify that ImageMagick recognizes the WebP delegation format:
    magick -version | grep -i webp
    (Or on ImageMagick 6: convert -version | grep -i webp). If webp appears in the delegates list, your installation is fully equipped.

Converting a Single Image to WebP

To convert an individual PNG or JPEG image to WebP with fine-tuned quality settings:

  1. Navigate to the folder containing your image:
    cd ~/Pictures/web-assets/
  2. Execute the conversion command specifying quality parameters:
    magick input.jpg -quality 82 -define webp:lossless=false output.webp
    • -quality 82: Balances aggressive compression with visual fidelity (values between 75 and 85 are optimal for web delivery).
    • -define webp:lossless=false: Applies lossy WebP compression suitable for photographs. For icons or illustrations requiring crisp vector-like edges, set this to true.

Batch Converting All Images in a Directory with a Bash Loop

When dealing with a folder containing numerous PNG or JPG files, a concise Bash for loop converts each file while preserving original basenames.

  1. Create an output directory to store your new WebP assets cleanly:
    mkdir -p webp_converted
  2. Run a loop to convert all JPEG images:
    for img in *.jpg; do
      [ -f "$img" ] || continue
      magick "$img" -quality 80 "webp_converted/${img%.jpg}.webp"
    done
  3. To batch convert both JPG and PNG files simultaneously, handle case extensions with a combined shell pattern:
    for img in *.{jpg,jpeg,png,JPG,PNG}; do
      [ -f "$img" ] || continue
      magick "$img" -quality 82 -define webp:method=6 "webp_converted/${img%.*}.webp"
    done
    The webp:method=6 parameter instructs the encoder to spend extra CPU cycles finding the smallest possible file size.

Accelerating Conversions on Multi-Core CPUs with GNU Parallel

If you need to convert thousands of high-resolution image files, standard Bash loops run sequentially on a single CPU core. Using GNU Parallel distributes jobs across all available processor cores for maximum throughput.

  1. Install GNU Parallel if it is not already available:
    sudo apt install parallel -y
  2. Run the conversion job across all CPU cores simultaneously:
    find . -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.png" \) | parallel -j+0 magick {} -quality 80 -define webp:method=6 {.}.webp
    • find . -maxdepth 1 ...: Discovers all image candidates in the current directory.
    • parallel -j+0: Spawns one worker process per CPU thread.
    • {.}: Automatically strips the source file extension and replaces it with .webp.

Verifying File Size Savings

Compare the cumulative file footprint of your original images against the generated WebP files:

du -sh *.jpg webp_converted/*.webp

You will typically see bandwidth savings ranging from 30% to over 60%, delivering faster page load metrics without compromising graphic quality.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.