How to recursively and automatically convert all bmp images to png files in a given directory?

I have a directory on my machine with 100s of images in it. About half of those images are bmp files, and the other half are png files. I need to convert all the bmps into pngs, but as there are so many of them I don't want to do it manually.

So how can I recursively and automatically (probably using a script) convert all the bmp image files into png image files in that directory?

I am running Ubuntu GNOME 15.10 with GNOME 3.18.


Solution 1:

A simple for loop might be enough for a single directory:

for i in *.bmp
do 
convert $i "${i%.bmp}.png"
done

To make this truly recursive there are a few choices, one method is the following:

find . -name '*.bmp' -type f -exec bash -c 'convert "$0" "${0%.bmp}.png"' {} \;

If you wish to dabble a bit more you specify a quality level for the png level by using the syntax:

-quality value

This takes a value of 1 for the lowest quality and smallest file size to 100 for the greatest quality and largest file size. The default is approximately 92. Further details here...

Solution 2:

I'd say the answer by andrew.46 is still the best, being it is an eloquent on-liner. However, here is another option. The only advantage is that there is a "current file number count" out of "total number of files" to convert, and it echoes the file being converted. You'll want to remove any spaces in file names though before running. This will remove spaces: find . -name "* *" | rename 's/ /-/g'

#!/bin/bash

cd $(pwd)    
bmp_files=$(find . -iname "*.bmp")

total=$(echo "$bmp_files" | wc -l)
num=0

echo "There are $total files to be converted."

for f in $bmp_files
do
    ((num++))
    echo "Converting $f, $num/$total"   
    convert "$f" "${f%.bmp}.png" 
    clear
done