Convert images from Windows Batch script

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
moet36
Posts: 1
Joined: 2018-01-29T19:41:57-07:00
Authentication code: 1152

Convert images from Windows Batch script

Post by moet36 » 2018-01-29T19:45:49-07:00

Hi

This is my first post here, so hope my questions is ok.

We use Imagemagick for batch processing images before using them on our website.

We currently use the following:
convert input.jpg -resize 1300x1300 -quality 60 -strip -colorspace sRGB output.jpg

Our images come in various formats. Our website is optimized for handling images with proportions 3:2 in both landscape and portrait format, i.e. 1300x867 and 867x1300.

I would like to automate so that all images are automatically resized to have this proportion of 3:2 (it should cut off equally in top/bottom or left/right when cropping). Not sure if Imagemagick can figure out whether it is supposed to be an portrait image (height > width) or landscape image (width > height) and then make the crop to the 3:2 (or 2:3) format accordingly and after that resize to have a max width/height of 1300 px ?

Can anyone help ?

Thank you in advance

User avatar
fmw42
Posts: 24024
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert images from Windows Batch script

Post by fmw42 » 2018-01-29T20:06:51-07:00

Please alway identify your version of ImageMagick as some syntax difference occur between IM 6 and IM 7

User avatar
fmw42
Posts: 24024
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert images from Windows Batch script

Post by fmw42 » 2018-01-29T20:52:13-07:00

If using IM 6.9.9.34 or IM 7.0.7-22, then there is a new feature in -crop that allows cropping to a given aspect ratio. You first have to decide if portrait or landscape. You can do that with

Code: Select all

convert image -format "%[fx:(w>h)?1:0]" info:
1 means landscape and 0 means portrait

The do a conditional test. That code is OS dependent for its syntax. I just use pseudo code here for the conditional

Code: Select all

if 1 (landscape); then
convert input.jpg -resize 1300x1300 -quality 60 -strip -colorspace sRGB -gravity center -crop 3:2+0+0 +repage output.jpg

else (portrait); then
convert input.jpg -resize 1300x1300 -quality 60 -strip -colorspace sRGB -gravity center -crop 2:3+0+0 +repage output.jpg

Otherwise, it becomes a bit more complicated, but can be done with more scripting.

User avatar
GeeMack
Posts: 647
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Convert images from Windows Batch script

Post by GeeMack » 2018-01-29T21:32:11-07:00

moet36 wrote:
2018-01-29T19:45:49-07:00
I would like to automate so that all images are automatically resized to have this proportion of 3:2 (it should cut off equally in top/bottom or left/right when cropping). Not sure if Imagemagick can figure out whether it is supposed to be an portrait image (height > width) or landscape image (width > height) and then make the crop to the 3:2 (or 2:3) format accordingly and after that resize to have a max width/height of 1300 px ?
Using IM6 on Windows 10 I worked up a command to crop an image to the largest output image of aspect ratio 3:2, or 2:3 if the input has a vertical orientation.

Code: Select all

convert input.png -write mpr:input ^
   -set option:distort:viewport "%%[fx:w<h&&w/2>h/3?h/3*2:w]x%%[h]" -distort SRT 0 ^
   -set option:distort:viewport "%%[fx:w>h&&w/3>h/2?h/2*3:w]x%%[h]" -distort SRT 0 ^
   -set option:distort:viewport "%%[w]x%%[fx:w<h&&w/2<h/3?w/2*3:h]" -distort SRT 0 ^
   -set option:distort:viewport "%%[w]x%%[fx:w>=h&&w/3<h/2?w/3*2:h]" -distort SRT 0 ^
   mpr:input +repage -gravity center -composite output.png
That reads the input image and stores a copy in the memory register "mpr:input". Next it does a series of tests and viewport resizing to create the required 3:2 or 2:3 canvas. Then it brings that saved input image back and composites it centered over the proportioned canvas. The result is as if you cropped the largest 3:2 (or 2:3) proportioned area from the center of the input.

A square input will be output as a horizontal 3:2 image.

Any other operators for resizing etc. can be added before the final output. If you "-resize 1300x1300" the output will keep its 3:2 (or 2:3) proportions and be limited to 1300 pixels in the longer direction.

The command is written for a BAT file. To use it at the command line you'd have to change all the double percent signs "%%" into singles "%".

Post Reply