Page 1 of 1

How did i resize a image to a fixed size?

Posted: 2008-01-15T05:13:32-07:00
by soulmachine
I have tesed that Image::sample, Image::scale, Image::resize, Image::zoom will keep the ratio of a image's weight and height, is there a simple way to resize a image to a fixed size? for example, resize a image of 400×800 to 128*128.
In http://www.imagemagick.org/Usage/resize/#resize:
If you want you can force "-resize" to ignore the aspect ratio and distort the image so it always generates an image exactly the size specified. This is done by adding the character '!' to the size.Unfortunately this character is also sometimes used by various UNIX and DOS command line shells. So you may have to escape the character somehow to preserve it.
convert dragon.gif -resize 64x64\! exact_dragon.gif
convert terminal.gif -resize 64x64\! exact_terminal.gif

how can I do this by using the C++ APIs?
thanks in advance :)

Re: How did i resize a image to a fixed size?

Posted: 2008-01-15T19:38:13-07:00
by soulmachine
at http://magick.imagemagick.org/viewvc/bi ... threv=8407 I find some texts as follows:
By default, the width and height are maximum values. That is, the image is expanded or contracted to fit the width and height value while maintaining the aspect ratio of the image. Append an exclamation point to the geometry to force the image size to exactly the size you specify. For example, if you specify 640x480! the image width is set to 640 pixels and height to 480. If only one factor is specified, both the width and height assume the value.

so I write a test program:
Magick::Image inputImage;
inputImage.read("D:\\green.jpg");
inputImage.resize(Magick::Geometry("256x256!"));
It succeed! This is the same as Image::sample,Image::scale, Image::zoom.

Re: How did i resize a image to a fixed size?

Posted: 2008-01-16T17:45:36-07:00
by anthony
The moral, is that SHELL and DOS interpret the '!' character.
C and C++ does not, so no backslash or quotes are needed.

Also The way IM interprets special flags like '!' and '%' allows them to appear ANYWHERE (except in the middle of a number) in geometry-type options. That is you can place a '%' or '!' in the middle, or at the beginning of the string, IM does not care. For example "!256x256" is equivalent to "256x256!".

As a secondary effect of this '%' is only a flag, and for geometry-like arguments, is not actually tied to a specific number or value in the geometry string. As such all these strings are also equivalent "100%x50%", "100x50%" "%100x50" "100x50%".

We may not think they are the same, but to IM they are interpreted to mean the same. :-(

Re: How did i resize a image to a fixed size?

Posted: 2008-01-22T17:16:05-07:00
by fmw42
If you are on Unix or Mac and want to resize to a square format, you can try my squareup bash script at
http://www.fmwconcepts.com/imagemagick/index.html