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

batchcommand linegnomeimage processingscripts

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.

Best Answer

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...