8

I have converted a single file from BMP to PNG with ImageMagick's convert using the command below

convert CD\ Front.bmp CD\ Front.png

I have many such files, thus I tried:

for f in */*.bmp ; do convert $f ${f%bmp}png; done

but this command hangs forever.

What am I doing wrong?

12

You most likely have a problem because you don't quote the file names:

for f in */*.bmp ; do convert "$f" "${f%bmp}png" ; done
#                             ^  ^ ^           ^

do this so the space in the file names do not cause problems.

  • 3
    $f should be quoted too – Mat Dec 15 '13 at 10:50
  • You are right, thank you for pointing out – Zelda Dec 15 '13 at 11:00
  • 1
    Quoting the variables is correct, but if you quote the glob then the shell will look for a literal */*.bmp. – evilsoup Dec 15 '13 at 12:04
  • Thanks to you as well. I should try out my solutions before posting... – Zelda Dec 15 '13 at 12:26
18

You can use mogrify:

mogrify -format png *.bmp

Source: Imagemagick website

1

I would refactor the code like this. I find this method to be clearer & easier to debug:

find . -type f -name '*.bmp' |\
  while read BMP
  do
    DIR=$(dirname "$BMP")
    PNG="$(echo $BMP | sed 's/.bmp//g')"
    convert "${BMP}" "${PNG}".png
  done
  • 1
    I took the liberty to change JPEG into PNG while reviewing – Anthon Dec 15 '13 at 17:09
  • Ahh, thanks. But it did work for JPEG’s as well! – JakeGould Dec 15 '13 at 17:10
  • 1
    Yes, guess it does, but it was not what the OP asked for ;-) – Anthon Dec 15 '13 at 17:21
  • Don't use find | while read, especially when a glob will suffice. – Kevin Dec 15 '13 at 18:17
  • And the sed would be better as 's/\.bmp$/.png/', then obviously remove the .png from the actual command – Kevin Dec 15 '13 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.