Resize an Image using ImageMagick C API

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
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Resize an Image using ImageMagick C API

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize an Image using ImageMagick C API

Post 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.)
snibgo's IM pages: im.snibgo.com
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Resize an Image using ImageMagick C API

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Resize an Image using ImageMagick C API

Post 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.
snibgo's IM pages: im.snibgo.com
him21sri
Posts: 36
Joined: 2016-05-19T05:27:37-07:00
Authentication code: 1151

Re: Resize an Image using ImageMagick C API

Post by him21sri »

Thanks for the help :)

Really Appreciate :)
Post Reply