ImageMagick on multiple files

5,412

Solution 1

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.

Solution 2

You can use mogrify:

mogrify -format png *.bmp

Source: Imagemagick website

Solution 3

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
Share:
5,412

Related videos on Youtube

Gilles 'SO- stop being evil'
Author by

Gilles 'SO- stop being evil'

Updated on September 18, 2022

Comments

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 1 year

    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?

  • Mat
    Mat over 10 years
    $f should be quoted too
  • Zelda
    Zelda over 10 years
    You are right, thank you for pointing out
  • evilsoup
    evilsoup over 10 years
    Quoting the variables is correct, but if you quote the glob then the shell will look for a literal */*.bmp.
  • Zelda
    Zelda over 10 years
    Thanks to you as well. I should try out my solutions before posting...
  • Anthon
    Anthon over 10 years
    I took the liberty to change JPEG into PNG while reviewing
  • Giacomo1968
    Giacomo1968 over 10 years
    Ahh, thanks. But it did work for JPEG’s as well!
  • Anthon
    Anthon over 10 years
    Yes, guess it does, but it was not what the OP asked for ;-)
  • Kevin
    Kevin over 10 years
    Don't use find | while read, especially when a glob will suffice.
  • Kevin
    Kevin over 10 years
    And the sed would be better as 's/\.bmp$/.png/', then obviously remove the .png from the actual command