How did i resize a image to a fixed size?

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
soulmachine

How did i resize a image to a fixed size?

Post 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 :)
soulmachine

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

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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. :-(
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
Post Reply