Most portable way to resample image?

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
jdccdevel

Most portable way to resample image?

Post by jdccdevel »

The application I am writing depends on relatively consistent output from the resample function. I need to be able to, as portably as possible, resample an image from 600 to 100 dpi. I do not want to mandate the user have a particlar version which may not be bundled with their distro. As such, I need to create consistent output with as many versions of ImageMagick as possible, and I am having issues...

Using version: "6.5.2-2 2009-05-06 Q16 HDRI"

This Command line:

Code: Select all

convert large.png -resample 100 small.png
Produces the same output (close enough for me) as this Perl Code:

Code: Select all

        $image = Image::Magick->new;
        $image->Read('large.png');
        $image->Set(units=>'PixelsPerInch');
        $image->Set(density=>600);
        $image->Resample(density=>100},
                        filter=>'Lanczos',
                        blur=>0.5);
        $image->Set(depth=>8);
        $image->Write(filename=>'small.png');
Yet on another machine, (Version "6.4.4 2009-02-13 Q16 HDRI") the output from the perl code is very different.

In particular, it appears the "filter" and/or "blur" settings are being ignored. (Which may be a bug in that version... but that doesn't help me much.)

If need be, I can write a C program (using the API) to do this, but I would rather not, as most of my code is Perl already. However, If I can guarantee that the output will be consistent, then it would be worth it.

Does The behavior of the command line change regularly? Does it diverge from the perl and C APIs often? or is this diversion a Bug? If I write some C code to do this, am I going to have the same problem as I have with the Perl API? (i.e. Output differing significantly from version to version?)

Thanks for the help.

Jonathan
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Most portable way to resample image?

Post by anthony »

blur in resample() is a new feature, and may not work in older IM's

The whole resize filter and the controls applied was re-written in IM v6.3.6-3, to make it more usable, more correct, and actually make some filter that did not work right, work as they were meant to work. Also provide extra controls and even a way of getting a data representation of the actual resize filter for graphing.

Details of the new system is in
http://www.imagemagick.org/Usage/resize/#filter

the specific feature blur represented is
http://www.imagemagick.org/Usage/resize/#filter_blur

Before the change is may have been mis-named as 'support' check with the older IM's documentation.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Most portable way to resample image?

Post by anthony »

Then you are best doing the size calculation yourself and calling Resize()
All Resample() does is set the new 'density', then resize the image so the new image is the same size in Real World Terms (not in pixel terms). Resize has not changed so its features are the same.

Also using Lanzcos (blur is not useful with a windowed-sinc filter), or a Gaussian like filter, such as Cubic or Quadratic (with blur control), or even just the Mitchell filter should still produce exactly the same results as very very old versions of IM. The major changes in the recent re-development was for some of the special windowed-sinc filters, like Blackman, which was incorrectly programmed.

In other words the most common filters have not changed.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Most portable way to resample image?

Post by anthony »

So you know the original images density.
So resize the image to 1/6 th the original size and add
-set density 100 -units PixelsPerInch
That is basically what -resample does!

Alternately..

Code: Select all

   convert read_image  -set density 600 -resample 100  write_image
It isn't rocket science!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply