44

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.

walther
  • 13,466
  • 5
  • 41
  • 67
Tal Galili
  • 24,605
  • 44
  • 129
  • 187
  • or if you use python look to this: http://stackoverflow.com/questions/2900035/changing-file-extension-in-python – Martijn van Wezel Mar 16 '15 at 11:22
  • 1
    @MartijnvanWezel the post you linked is not helpful. It details how to rename a file, not how to reformat it entirely. – speedstyle Mar 23 '18 at 06:35
  • 1
    @speedstyle oeww nasty, back in the days I didn't really know stuff... . I will let my command stand, so other people will learn from the mistake. – Martijn van Wezel Mar 26 '18 at 22:09

3 Answers3

79

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.

nwellnhof
  • 32,319
  • 7
  • 89
  • 113
  • 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?) – Tal Galili Mar 10 '13 at 22:21
  • 5
    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. – nwellnhof Mar 10 '13 at 22:33
  • 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 Jul 21 '13 at 07:54
  • 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... – nutty about natty Jul 21 '13 at 07:55
  • 1
    If `mogrify` is used with the `-format` option, the file is not replaced, but a new one is saved with the new prefix. – miro marchi Feb 14 '17 at 17:55
  • 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. – AnnanFay Jan 10 '18 at 20:05
40

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
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 2
    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. – jan-glx Sep 11 '19 at 11:37
  • For the second step just do `mv *.xbm converted` – lenooh Aug 03 '22 at 20:50
6

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?

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743