Shell script to conditionally resize an image
Posted: 2015-11-19T04:03:57-07:00
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?
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?