Page 1 of 1

Shell script to conditionally resize an image

Posted: 2015-11-19T04:03:57-07:00
by psoliver92
Hi, I am new to ImageMagick but not programming and manual image manipulation.

I'm interested in using ImageMagick because of the command line features. I want to write a shell script that will look at all the images in a certain directory and scale or crop them to fit certain image dimensions.

I see it working this way:

look at each image and calculate the ratio of width to height.

If it's 1:1 tag the image as square.

if it's > 1:1 tag the image as horizontal rectangle

if it's < 1:1 tag the image as vertical rectangle

the script would then perform the manipulation on that image determined by it's shape. E.g. for the square images I need, I would just scale the square images up or down, scale the horizontal rectangles by the width and extend the canvas by the different between the new scale height and the new width and vice versa for vertical rectangles.

The full list of dimensions I need from one image is

640x1136
320x480
480x720
640x960
114x114
120x120
144x144
177x177
258x258
29x29
36x36
40x40
48x48
50x50
512x512
57x57
58x58
60x60
72x72
76x76
775x775
80x80
96x96
264x95
517x186

tl;dr

I want to write a script to look at an image and scale it to fit the required dimensions as close as possible and then extend the canvas to fit the required dimensions. Then I want it to do that for every dimension in my list of required dimensions.

How would I go about this?

Re: Shell script to conditionally resize an image

Posted: 2015-11-19T10:23:06-07:00
by fmw42
What platform and version of IM are you using? If on unix, then see my shell scripts aspect and aspectpad. I do some of those things there. Once you have your aspect conditionals, then you can write every size image out in one command by the technique shown in http://www.imagemagick.org/Usage/files/#write

see also
http://www.imagemagick.org/Usage/thumbnails/#creation
http://www.imagemagick.org/script/comma ... p#geometry
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#clone

Scripting and syntax is different on Windows than unix, so also see
http://www.imagemagick.org/Usage/windows/