I am trying to make a batch file to compress and resize some images. They are JPG and PNG. The files vary is size, but I only want to modify the large files.
If possible, I would like an answer to each option I am exploring.
Option 1: Simple
As the description above sounds, I used some basic commands to achieve this.
@echo off
for %%f IN (*.jpg *.png) DO magick mogrify -quality 70 -resize 960x540^\> "%%f" "%%f"
I believe the issue is with ^\>
. I only want it to resize larger files and to fit to the smallest dimension. However, I can't find a way to have both conditions present. Also, I can't get just >
to work (so I'm not sure if I am off track).
Option 2: Complicated
This guy completed a fairly decent analysis on the best method for resizing (and compressing) images. This led me to join it to my code to create...
@echo off
for %%f IN (*.jpg *.png) DO magick mogrify -filter Triangle -define filter:support=2
-thumbnail 500 -unsharp 0.25x0.08 8.3 0.045 -dither None -posterize 136 -quality 82
-define jpeg:fancy-upsampling=off -define png:compression-filter=5
-define png:compression-level=9 -define png:compression-strategy=1
-define png:exclude-chunk=all -interlace none -colorspace sRGB "%%f" "%%f"
[Manual breaks made for better viewing.]
The only thing I can't get working here is the condition only changing files greater than 500 pixels (>
). This may be the same issue I was having above.
I hope this is a simple solution. I did try to look in the documentation, but I didn't find anything particularly helpful (not more than the code informing me of conditions).
I appreciate you assistance.
CodePudding user response:
In DOS Windows in Imagemagick, see https://legacy.imagemagick.org/Usage/windows/#conversion where it says:
All reserved shell characters which are not in double quotes must be escaped with a '^' (caret or circumflex) if used in a literal sense (i.e. not fulfilling their usual purpose). These reserved shell characters are: '&', '|', '(', ')', '<', '>', '^'.
This especially means that: The special character '>' (used for resize geometry) needs to be escaped using '^'. For example -resize 100x100^>.
Similarly the 'internal fit resize' flag '^' needs to be doubled to become '^^'.
So you can either use double quotes (and no escapes) as:
-resize "960x540^>"
or you can escape both the ^ and the > (and no quotes) as:
-resize 960x540^^^>