How to convert all files (in different formats) in given folder to different file type

48,982

Solution 1

Try the mogrify command:

mogrify -format png *.*

But be careful. Without the -format option, mogrify overwrites the original images. Make sure to read the documentation.

Solution 2

Although mogrify seems to do the job, I would like to show you how this can be done with multiple commands with convert from ImageMagick.

I think multiple commands are better, because the number of file types is supposedly quite small and you can better adjust it to your needs:

This command:

for file in *.xbm; do convert $file "`basename $file .xbm`.png"; done

will convert all .xbm files to .png while not touching the xbm files.

Then you can move all "converted" files:

mkdir converted
for file in *.xbm; do mv $file converted/; done

Solution 3

You can use convert command from ImageMagick package, e.g.

find . -maxdepth 1 \( -iname \*.png -o -iname \*.jpg -o -iname \*.jpeg -o -iname \*.bmp -o -iname \*.pdf \) -exec convert -verbose "{}" "DEST_DIR/{}.png" \;

Or if you've got all the files in the same directory, try the following simpler way:

convert '*.*' converted_%04d.png

Similar: How can I scale all images in a folder to the same width?

Share:
48,982
Tal Galili
Author by

Tal Galili

Statistics, blogging, and the hope for a happy long life.

Updated on December 12, 2020

Comments

  • Tal Galili
    Tal Galili over 3 years

    I have a folder with many images from different types (png, jpg, jpeg, bmp, pdf), and I would like to convert them all into png (for instance) using imagemagick.

    Is there a single command which can perform this? If not, what should I do instead?

    Thanks.

  • Tal Galili
    Tal Galili over 11 years
    Thanks. I get the following error massage: C:\Users\Tal\s>mogrify -format png . mogrify.exe: unable to open module file C:\Program Files (x86)\ImageMagick-6.8. 3-Q16\modules\coders\IM_MOD_RL_INI_.dll': No such file or directory @ warning/mo dule.c/GetMagickModulePath/683. mogrify.exe: no decode delegate for this image format desktop.ini' @ error/cons titute.c/ReadImage/550. (Is this simple to resolve?)
  • nwellnhof
    nwellnhof over 11 years
    That's because mogrify tries to convert the file desktop.ini which isn't an image file. You should only pass image files to mogrify. Either delete desktop.ini or change *.* to a wildcard that only matches the image files in your directory. You can also run the command several times for every file format: mogrify -format png *.jpg, mogrify -format png *.bmp, etc.
  • nutty about natty
    nutty about natty almost 11 years
    I'd like to merge different image file types into a single *.pdf file. How to adapt your code to make that work?
  • nutty about natty
    nutty about natty almost 11 years
    I guess I could revert to other tools like pdftk (e.g. by using pdftk *.pdf cat output combined.pdf), but would rather have a one-liner to get the whole job done...
  • miro marchi
    miro marchi over 7 years
    If mogrify is used with the -format option, the file is not replaced, but a new one is saved with the new prefix.
  • AnnanFay
    AnnanFay over 6 years
    If mogrify is used with the -format option using the original file format it will still overwrite the original. Not sure if there's anyway to avoid this.
  • jan-glx
    jan-glx almost 5 years
    The current command does not work if the filename contains spaces. One way to fix this is for file in *.xbm; do convert "$file" "`basename \"$file\" .xbm`.png"; done but I have a feeling that bash offers a cleaner solution for this.