7

I thought there was a simple switch in mogrify command options that tells it go through subfolders as well. I didn't find any. On Linux, the "find" command seems to do the trick, how do you do this on Windows?

magick mogrify -resample 72 -resize 700x700 -format png -path "C:\ImageMagick-7.0.8-Q16\dest" *.*

works but I need it to recurse through subfolders as well. What's the simplest way to do that?

Ideally, I'm looking for the source directory structure to be retained in destination but with the processed images only.

Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
Regmi
  • 845
  • 3
  • 15
  • 30

3 Answers3

9

ImageMagick Mogrify Files Recursively - Windows

You can use a FOR /R loop to iterate the files starting from the root directory the files reside and then run those over the commands accordingly as per the below example against each file.

If you omit the -path parameter it seems that it will run against the original files where they reside. So for a simple solution to retain the original structure from the original source, consider simply copying the original root folder of the source files to a new location and then rename that folder.


Example Command

Note: Be sure to replace C:\Source\Folder\Root to be the exact folder where the files or file subfolders reside that it will recurse to run the command operations against those files.

for /r "C:\Source\Folder\Root" %a in (*.*) do mogrify -resample 72 -resize 700x700 -format png "%~a"

Example Command (with delete)

for /r "C:\Source\Folder\Root" %a in (*.*) do mogrify -resample 72 -resize 700x700 -format png "%~a" && IF NOT [%~Xa]==[.png] DEL /Q /F "%~a"

Further Resources

  • FOR /R
  • Batch Substitutions (FOR /?)

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~xI        - expands %I to a file extension only
    
  • mogrify

  • Del
Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
1
dir -recurse -path "D:\DOWNLOAD" -include *.jpg | %{magick $_.FullName -quality 95% -interlace Plane -sampling-factor 4:2:0 -strip "$($_.Directory)\$($_.Basename).webp"}

Powershell solution, change the factor as you wish.

It quite a pain to type all these factors, for powershell you can setup a function that "format" your command:

    function magickrec($tpath, $quality,$sformat ,$tformat)
{
    dir -recurse -path $tpath -include *.$sformat | %{magick $_.FullName -quality $quality -interlace Plane -sampling-factor 4:2:0 -strip "$($_.Directory)\$($_.Basename).$tformat"}
}

Save it in your default powershell folder(eg.D:\DOCUMENT\WindowsPowerShell) as Microsoft.PowerShell_profile.ps1 and set your "ExecutionPolicy" as "RemoteSign". (To do this, you need to run ps as adminstrater, then set-ExecutionPolicy RemoteSigned)

By typing magickrec "yourpath" 76% jpg heic you can turn all jpg image in that folder recursivly into heic. As before, you change the factor as you wish.

0

The existing Windows answer gives only a single file at a time to mogrify, which probably slows it down since it cannot multithread with OpenMP in the same way.

Here is my answer formatted for a batch script. It mogrifies all matching files in the current directory, and then also in all subdirectories (recursively). Save the batch script in your desired directory and then double-click to execute it.

@echo off
echo MOGRIFY CURRENT DIRECTORY:
@echo on
magick mogrify -transparent white -trim +repage *.png
@echo off
echo:
echo MOGRIFY SUBDIRECTORIES:
@echo on
FOR /D /R %%A IN (*) DO magick mogrify -transparent white -trim +repage "%%A\*.png"
@echo off
echo:
echo SCRIPT COMPLETE
pause