I recently had to process a large number of images in a couple of ways so I thought I’d log them down here.

Convert a folder’s worth of PNG files into WebP…

cd /to/the/folder
find . -type f -name "*.png" -print0 | xargs -0 -I {} convert {} -quality 95 {}.webp \
&& rename 's/\.png\.webp$/\.webp/' *.webp

The command above will find all PNG images in a folder, convert them into WebP at a quality level of 95 and output them with the same name that they originally had but with the webp extension instead. This is handy so that you can see the PNG version and WebP version side by side.

Note that you will need ‘imagemagick’, ‘webp’ and the ‘rename’ packages installed for this. I installed them using

brew install imagemagick webp rename

Remove the transparent background from a PNG and crop the remaining image…

find . -type f -name "*.png" -print0 | xargs -0 -I {} convert {} -trim +repage {}.png