2

Windows 10. ImageMagick v7.1.0-62

I need to compress and optimise a lot of .jpg images using ImageMagick. I am using the following command, which is working (this overwrites the original files which is what I want).

I run this in the windows command line when in the required folder (e.g. folder 1)

magick morgify -gaussian-blur 0.05 -colorspace RGB -strip -quality 50 -interlace Plane *.jpg

The issue is this command only handles a single folder at a time. how can I change this so it runs recursively from my root folder through all sub folders.

My file structure is below;

- images
- - folder 1
- - - folder 1a
- - - folder 1b
- - folder 2
- - folder 3
- - - folder 3a
- - - folder 3b
jonboy
  • 231
  • 1
  • 5
  • 14
  • 1
    [For - Loop through command output - Windows CMD - SS64.com](https://ss64.com/nt/for_cmd.html) – DavidPostill Feb 22 '23 at 09:55
  • Thanks @DavidPostill could you provide me an example of how I could apply this to my current command please? – jonboy Feb 22 '23 at 09:59
  • 1
    There are examples in the link I gave you. – DavidPostill Feb 22 '23 at 10:28
  • @DavidPostill I can't seem to get it working recursively through folders - only individual ones. Maybe there's a limitation with using `morgify ` in a for loop, perhaps I need a custom script. I'll keep investigating.. – jonboy Feb 22 '23 at 14:41

1 Answers1

2

You can just loop directories. In PowerShell it's as simple as:

Get-ChildItem -Directory -Recurse | % { mogrify -gaussian-blur 0.05 -colorspace RGB -strip -quality 50 -interlace Plane "$($_.FullName)/*.jpg" }

It gets all the subdirectories, then uses ForEach-Object (%) to go through returned results and calls mogrify command for each.

Note that mogrify will throw an error if one of the subdirectories doesn't have any jpgs, but the command will continue for all subdirectories regardless.

It also doesn't include the folder where you're calling the command, but you can create a .ps1 script with the code above and include also mogrify -gaussian-blur 0.05 -colorspace RGB -strip -quality 50 -interlace Plane *.jpg command in it to run it there, then execute the script.

Destroy666
  • 5,299
  • 7
  • 16
  • 35