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
Related videos on Youtube
Author by
Gilles 'SO- stop being evil'
Updated on September 18, 2022Comments
-
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 belowconvert 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 over 10 years
$f
should be quoted too -
Zelda over 10 yearsYou are right, thank you for pointing out
-
evilsoup over 10 yearsQuoting the variables is correct, but if you quote the glob then the shell will look for a literal
*/*.bmp
. -
Zelda over 10 yearsThanks to you as well. I should try out my solutions before posting...
-
Anthon over 10 yearsI took the liberty to change JPEG into PNG while reviewing
-
Giacomo1968 over 10 yearsAhh, thanks. But it did work for JPEG’s as well!
-
Anthon over 10 yearsYes, guess it does, but it was not what the OP asked for ;-)
-
Kevin over 10 yearsDon't use
find | while read
, especially when a glob will suffice. -
Kevin over 10 yearsAnd the sed would be better as
's/\.bmp$/.png/'
, then obviously remove the .png from the actual command