Resize to a fixed geometry but maintain 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
smithmarkl

Resize to a fixed geometry but maintain aspect ratio

Post by smithmarkl »

Hello,

I need to convert an image from the command line to a fixed sized, but maintain the aspect ratio of the image content. This gets me close:
convert -resize 256x256 infile.jpg outfile.jpg

The problem being that I end up with one dimension that is less than 256 pixels. What I need is to fill in that space with black or transparent pixels above and below the image content to bring that dimension up to 256 pixels.

Any ideas on command line arg combinations to try?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize to a fixed geometry but maintain aspect ratio

Post by fmw42 »

smithmarkl wrote:Hello,

I need to convert an image from the command line to a fixed sized, but maintain the aspect ratio of the image content. This gets me close:
convert -resize 256x256 infile.jpg outfile.jpg

The problem being that I end up with one dimension that is less than 256 pixels. What I need is to fill in that space with black or transparent pixels above and below the image content to bring that dimension up to 256 pixels.

Any ideas on command line arg combinations to try?

see my tidbits page http://www.fmwconcepts.com/imagemagick/ ... ize_square
smithmarkl

Re: Resize to a fixed geometry but maintain aspect ratio

Post by smithmarkl »

That was very helpful. This does the trick:
convert -resize 256x256 infile.jpg -background none -gravity center -extent 256x256 outfile.jpg
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Resize to a fixed geometry but maintain aspect ratio

Post by anthony »

Generally this is know and thumbnail padding
http://www.imagemagick.org/Usage/thumbnails/#pad

Also please start reading the image before working on it, or you will have problems in the future.

EG: convert {read options} image {conversion operators} save_image
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply