6

cwebp - Compress an image file to a WebP file

-resize width height

Resize the source to a rectangle with size width x height. If either (but not both) of the width or height parameters is 0, the value will be calculated preserving the aspect-ratio.

The -resize option resizes the image to width but I want resize to occur only if image is larger than specified width x height.

1 Answers1

1

For those who are searching, use this snippet

Note that it requires to install ImageMagick for getting image size

#!/bin/bash

On Debian/Ubuntu: sudo apt-get install imagemagick webp

for image in images/.jpg; do if [[ ! -e $image ]]; then continue; fi size=(identify -format '%w %h' $image) if [ ${size[0]} -gt ${size[1]} ]; then if [ ${size[0]} -gt 700 ]; then cwebp -q 50 -resize 700 0 -mt $image -o ${image%.}.webp else cwebp -q 50 -mt $image -o ${image%.}.webp fi else if [ ${size[1]} -gt 700 ]; then cwebp -q 50 -resize 0 700 -mt $image -o ${image%.}.webp else cwebp -q 50 -mt $image -o ${image%.*}.webp fi fi rm $image done

Vlad
  • 111
  • Welcome to the site, and thank you for your contribution. May I recommend, however, to make some modifications to the coding style: The shell variables should always be quoted unless you know exactly that you need word-splitting by the shell (particularly important when dealing with filenames). Also, the "backtick-style" for command substitutions is deprecated, and the new $( ... ) style is now the recommended way of doing this. – AdminBee Jul 31 '20 at 09:45
  • @AdminBee Can you edit the answer or suggest the right style with '$(...)' and explain your meaning with shell variable quotes? – Adeerlike Sep 09 '20 at 21:17