7

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.

2 Answers 2

12

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

4
  • 1
    You missed the recursive part here.
    – kos
    Mar 24, 2016 at 23:53
  • This will probably do, but the recursive part would be a nice addition.
    – user364819
    Mar 24, 2016 at 23:59
  • Oops.... recursive added... Several methods to do this, this particular one is neat and easily understandable...
    – andrew.46
    Mar 25, 2016 at 0:25
  • Can you explain what the trailing {} \; means?
    – Maarten
    Mar 7, 2020 at 23:08
3

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
3
  • I see you posted another answer which you then deleted, you know you can just edit your answer as opposed to deleting it and writing a new one... And that would probably be advised too unless you put something too embarrassing in your previous answer. ;-)
    – user364819
    Mar 26, 2016 at 12:35
  • @ParanoidPanda Good point. I should have just edited it. I was thinking I was the only one who could see my deleted post. However, I've just read that "developers, moderators, and users with over 10,000 reputation" can view it, askubuntu.com/help/deleted-answers. I wonder if a moderator can fully remove it since it adds nothing in light of my other answer?
    – jbrock
    Mar 26, 2016 at 18:43
  • @ParanoidPanda Fortunately, nothing too embarrassing in that deleted post. ;) It is just a less elegant option compared to andrew.46's solution that I had posted before he posted the recursive part of his answer.
    – jbrock
    Mar 26, 2016 at 18:47

You must log in to answer this question.