Page 1 of 1
Resize an Image using ImageMagick C API
Posted: 2016-05-19T05:36:44-07:00
by him21sri
My ultimate target is to resize many images maintaining their aspect ratio as well, and if required fill the extra space(padding) with white color.
Please suggest some C API to achieve this.
I tried using MagickResizeImage and MagickScaleImage but both are resulting in blurry images.
When I use "convert -resize 1200x627 Desktop/myntraProd.jpg -gravity center -extent 1200x627 Desktop/myntraProdIMCLI.jpg" this from command line it gives me the desired output.
In short is there a way to resize an image while maintaining the aspect ratio as well.
Any help would be appreciated.
Re: Resize an Image using ImageMagick C API
Posted: 2016-05-19T06:10:58-07:00
by snibgo
I use these, in resize.h:
Code: Select all
*ResizeImage(const Image *,const size_t,const size_t,const FilterTypes,
const double,ExceptionInfo *),
*SampleImage(const Image *,const size_t,const size_t,ExceptionInfo *),
*ScaleImage(const Image *,const size_t,const size_t,ExceptionInfo *),
They all need the new number of columns and rows, so I need to calculate those.
They work fine, just like the command-line interface, as far as know.
(I mostly use MagickCore. If you use MagickWand, I expect that has suitable wrappers.)
Re: Resize an Image using ImageMagick C API
Posted: 2016-05-19T06:15:45-07:00
by him21sri
How do you calculate the new number of columns and rows for the above mentioned methods.
Say, for example I have an image with 1080x1440 dimension and I wanna resize it to 1200x627. So what should be the number of rows and columns that I need to pass to the above methods, so that I get a proper Image with keeping the aspect ratio as well.
Re: Resize an Image using ImageMagick C API
Posted: 2016-05-19T06:25:10-07:00
by snibgo
Your input image is 1080x1440. You want to resize it to fit into a box that is 1200x627, keeping the aspect ratio. Is that correct?
1200/1080=1.111
627/1440=0.4354
min(1.111, 0.4354) = 0.4354. So that is the magnification factor.
1080 * 0.4354 = 470.232
1440 * 0.4354 = 626.976
Round to the nearest integers, and check that neither is zero: resize to 470x627 pixels.
Re: Resize an Image using ImageMagick C API
Posted: 2016-05-19T06:35:36-07:00
by him21sri
Thanks for the help
Really Appreciate