Shell script to conditionally resize an image

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
psoliver92
Posts: 1
Joined: 2015-11-19T03:47:21-07:00
Authentication code: 1151

Shell script to conditionally resize an image

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Shell script to conditionally resize an image

Post 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/
Post Reply