Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick batch convert all PNGs in subdirectories to JPEGs

I have a folder with a number of sub folders in it which contain the images I want to convert from PNG to JPEG.

I have tried:

cd d:\images

mogrify -format jpg \*\*.png

I tried slashes in both directions (I'm using Windows 7).

But I just get invalid argument errors.

What should I do?

I'd really most value a simple example answer rather than a link to a 50 page image ImageMagick documentation if possible (I'm a newbie).

like image 951
Skyfish Avatar asked Apr 12 '15 09:04

Skyfish


4 Answers

Try using the following at the cmd prompt:

mogrify -format jpg *.png

I wasted hours trying use the ImageMagick convert command in a batch file, but couldn't get it to work

like image 62
sjc Avatar answered Oct 08 '22 00:10

sjc


You could always perform a for-loop:

cd D:\images
for /r /d %%a in (*) do mogrify -format jpg "%%~a\*.png"

Which will run the command for every sub-folder such that it is:

mogrify -format jpg "D:\images\name of subfolder\*.png"

Which appears to meet your requirements.

To use this code in command prompt replace %%a with %a

Edit

To use this code as it is you would need to put it in a batch-file. A very simple procedure for this is to:

  1. Open Notepad or any other text editor
  2. Copy and paste the code into it
  3. Save as and when naming it call it "something.bat"
  4. Before clicking save make sure you set the file-type to All Files (*.*) in the drop-down menu below the name.
  5. Whenever you want to perform the operation simply find "something.bat" or whatever you named it and double click to run.

That way you don't have to open cmd every time you want to perform the action.

like image 44
Monacraft Avatar answered Oct 08 '22 01:10

Monacraft


ImageMagick mogrify will not traverse directories. You will have to write a script loop over each directory that you want. Then in the loop change directories and run mogrify -format jpg *.png for each directory. I also recommend that you either backup your directories or use -path to set a path to your new but empty output directories.

like image 34
fmw42 Avatar answered Oct 08 '22 01:10

fmw42


For novice:

Download ImageMagick from below link:

https://www.imagemagick.org/script/download.php

then just copy and paste below command in cmd:

for /r /d %a in (*) do "C:\Program Files\ImageMagick-7.0.6-Q16\magick.exe" mogrify -format png "%~a\*.jpg"

The above command is working fine for me which converts all files from JPGs to PNGs, present in the current directory.

And then if you want to remove all the residual JPGs, just hit the below command:

for /r %i in (*.jpg) do del "%i"
like image 29
Jay Thaker Avatar answered Oct 08 '22 01:10

Jay Thaker