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
Convert images from Windows Batch script
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert images from Windows Batch script
Please alway identify your version of ImageMagick as some syntax difference occur between IM 6 and IM 7
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert images from Windows Batch script
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
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
Otherwise, it becomes a bit more complicated, but can be done with more scripting.
Code: Select all
convert image -format "%[fx:(w>h)?1:0]" info:
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.
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Convert images from Windows Batch script
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.moet36 wrote: ↑2018-01-29T19:45:49-07:00I 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 ?
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
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 "%".