Different Canvas sizes

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
normaldenker
Posts: 3
Joined: 2009-02-12T09:46:11-07:00

Different Canvas sizes

Post by normaldenker »

Hello,

I was searching a lot but did not find a solution for the following problem:

My original jpeg has a size of, e.g., 1024/768.

I want to resize and crop to different sizes. In three steps I was trying to transform it to 80px/50px - 480/280 - 150/150.

For resizing I already tried several commands, this for example:
resize 480x280 -gravity Center -crop 480x280+0+0

or

resize 480x280 -gravity Center -crop 480x280+0+0 +repage

The problem is: Using the "+repage"-command nothing happens ...

I don't want to have a white border. The picture should be resized in the best way possible.

Hope you understand what I mean.

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

Re: Different Canvas sizes

Post by fmw42 »

+repage only affects the virtual canvas and does nothing if your output is jpg.

-resize 480x280 will preserve the aspect ratio of your input image. So if you aspect ratio of your input does not match that of your resize dimensions then it will choose to match one dimension and have the other smaller than specified. So to get the image to be the size you want you would need to pad with some color using -background somecolor -gravity center -extent 480x280, e.g.

convert yourimage -resize 480x280 -background white -gravity center -extent 480x280 resultimage

However the other choice if the two do not have the same aspect ratio is to crop. That is done by using the ^ in the resize, so that it resizes to the smaller size and leaves the other dimenion bigger than desired.

convert yourimage -resize "480x280^" -background white -gravity center -crop 480x280+0+0 +repage resultimage

The +repage is needed for example for png or gif output, but not for jpg, so that the virtual canvas is made the same size as the cropped image and not left as the size after the resize.

The other choice is to tell the resize that you want the exact size specified, but then you must be willing to accept distortion as it stretches on or the other dimension.

convert yourimage -resize "480x280!" resultimage

see
http://www.imagemagick.org/script/comma ... p#geometry

You did not say what version of IM you are using and what platform. Some of these options are not available on very old systems. For windows users also see syntax differences at http://www.imagemagick.org/Usage/windows/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Different Canvas sizes

Post by anthony »

See the thumbnail resize and pad methods in...
http://www.imagemagick.org/Usage/thumbnails/#fit
and specifically the summery at
http://www.imagemagick.org/Usage/thumbn ... it_summery

If you don't care about the aspect ratio at all, but want to stretch/compress the width and height un-equally use a '!' flag with the resize
http://www.imagemagick.org/Usage/resize/#noaspect

Both sections of IM examples, thumbnails and resize, are a good read if you really want to control exactly what you are wanting to get.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply