Resizing and Padding to keep a constant aspect ratio

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
manmath
Posts: 4
Joined: 2014-08-31T19:06:24-07:00
Authentication code: 6789

Resizing and Padding to keep a constant aspect ratio

Post by manmath »

Hi, I'm badly need help. Here's is the problem at hand.

#1 I've some 2600 pics to upload in the product catalog of a website
#2 The pics are of very high resolutions (> 4000px in height), but their aspect ratio is not same
#3 I want their aspect ratio to be 1:1.5, i.e., 1000x1500 px. I don't want to crop any, but put padding whenever necessary to maintain the said aspect ratio.
#4 If possible also suggest me how recursively resize all the images.

Suggest me through a simple example.

Thanks in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing and Padding to keep a constant aspect ratio

Post by fmw42 »

Code: Select all

convert image -resize 1000x1500 -background somecolor -gravity center -extent 1000x1500 resultimage
If you want to process all the images in a given directory, you can use mogrify

cd to directory containing images

Code: Select all

mogrify -resize 1000x1500 -background somecolor -gravity center -extent 1000x1500 *
But I suggest you create a new directory and use -path in mogrify so that you do not write over your source images. If you want the convert to some new image format, then use -format.

See
http://www.imagemagick.org/Usage/basics/#mogrify

If you have multiple directories, you will need to write a script to loop over each directory and use convert on a list of files in each directory or use mogrify.
manmath
Posts: 4
Joined: 2014-08-31T19:06:24-07:00
Authentication code: 6789

Re: Resizing and Padding to keep a constant aspect ratio

Post by manmath »

Thanks. That worked like a charm. Just one issue is unresolved - how to process images recursively, i.e., the images in the subdirectories.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing and Padding to keep a constant aspect ratio

Post by fmw42 »

manmath wrote:Thanks. That worked like a charm. Just one issue is unresolved - how to process images recursively, i.e., the images in the subdirectories.
You have to write a script (that depends upon your OS) to loop over each subdirectory that you want to process and do mogrify on each.

You have not provided what version of IM you are using nor what OS.
manmath
Posts: 4
Joined: 2014-08-31T19:06:24-07:00
Authentication code: 6789

Re: Resizing and Padding to keep a constant aspect ratio

Post by manmath »

I'm working on ImageMagick-6.7.8.9-10.el7.x86_64 on CEntOS 7.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resizing and Padding to keep a constant aspect ratio

Post by fmw42 »

manmath wrote:Thanks. That worked like a charm. Just one issue is unresolved - how to process images recursively, i.e., the images in the subdirectories.
What was your exact command line?
manmath
Posts: 4
Joined: 2014-08-31T19:06:24-07:00
Authentication code: 6789

Re: Resizing and Padding to keep a constant aspect ratio

Post by manmath »

mogrify -resize 1000x1500 -background white -gravity center -extent 1000x1500 *
Post Reply