Page 1 of 1

Image Composites and Re-sizing

Posted: 2013-03-20T21:28:11-07:00
by zseml
Using convert.exe, I'm trying to trim an image and then center the image on a particular canvas size.

For example, I have a source image that's 200x100. The result I want is for the final image to have a canvas size of 100x100 and the original source image to be re-sized to 100x50 and to be centered on the 100x100 canvas.

The issue I'm running into is that the canvas size always shrinks down to 100x50 and I can't get it to stay at 100x100. Furthermore, I'm processing a lot of images and all source images are variable dimensions/aspect ratios (so I can't just hard-code some geometry flags).

Thoughts?

Re: Image Composites and Re-sizing

Posted: 2013-03-20T21:37:42-07:00
by fmw42
What is your command line? What is your IM version?

How do you decide on the size for -resize, if all images are different?

If you only have one image and want a constant color background, then you need to resize to the larger dimension and use -extent to fill it out.

convert inputimage -resize 100x50 -gravity center -background white -extent 100x100 outputimage

or

convert inputimage -resize 50% -gravity center -background white -extent 100x100 outputimage

or

convert inputimage -resize 100x100 -gravity center -background white -extent 100x100 outputimage

This last one will resize the larger dimension of your image, whichever it is, to 100 and the other dimension proportionally. Since the other dimension is smaller it will end up less than 100 (whatever value keeps the aspect ratio). I suspect this is what you want.


see
http://www.imagemagick.org/script/comma ... php#extent
http://www.imagemagick.org/Usage/thumbnails/#pad (and the sections following that)


If that is not what you want, please clarify further and provide your command line.

Re: Image Composites and Re-sizing

Posted: 2013-03-20T21:58:35-07:00
by zseml
Thanks for your reply. My IM version is 6.8.3.

The command you provided is very close to what I'm looking for.

The command I'm running is:

convert image.tif -gravity center -trim -resize 173x159 -extent 173x159 output.png

The one part I left out is that I want to preserve vertical whitespace, but trim any horizontal whitespace. So, in other words, my canvas height will always be 159, but my width might be less than that (but it'll max out at 173).

It's almost like I need an "if" clause after my -resize happens to conditionally run the "extent" command to fill vertical space.

Does that make sense?

Re: Image Composites and Re-sizing

Posted: 2013-03-21T13:55:00-07:00
by fmw42
You an trim on only one side or any number of sides by using the technique at http://www.imagemagick.org/Usage/crop/#trim_oneside

also see my unix bash shell script, trimmer, at the link below