Bulk Convert to Modern Image Format with Image Magik Script

Apparently the days of good ol PNG are over. Or at least that’s what the site optimization scanners are telling me. To blindly adhere and increase my SEO I’m just going to straight convert all my images to a modern format. I chose WebP Because it’s apparently the easiest blind choice for web these days.


Install ImageMagik from here, make sure it is added to your PATH variable in whatever shell you’re using. We’re going to need convert.

If you’re on UNIX or MacOS you’ll also need to install the webp from libwebp.


I wanted a script that would recursively go through every subdirectory and convert any found images to WebP. Here’s what I came up with:
- find -follow creates a nice iteration of all the files below pwd.

Note: This deletes the old files, do ensure you have a backup before you try this, or just remove the deletion line rm $f.

for f in `find $1 -follow`; do 
    dir=$(dirname $f)
    filename=$(basename -- "$f")
    if [ "$ext" == "png" -o "$ext" == "PNG" -o "$ext" == "jpg" -o "$ext" == "JPG" ]; then
        echo "Converting $f"
        convert "$f" "$dir/$filename.webp"
        echo "Done $filename.webp"
        rm $f
        if [ ! -d "$f" ]; then
            printf "Error:\n Unsupported format: $f\n"

I added this to the root of my web repo so I can eventually stuff it into a CI job. I really don’t want to have to think about converting screenshots to this format all the time!

$ cd content && tree