toreau
toreau

Reputation: 660

Why do the color profile/spaces of my PNG images change when processed with Perl's Image::Magick?

I have encountered some quite puzzling behaviour with ImageMagick (using PerlMagick: I open a PNG picture, resize it, and saves it.

All good, except that the resulting image colors' are slightly different.

# Create the ImageMagick object.
my $magick = Image::Magick->new;

eval {
    $magick->BlobToImage( $image );
};

$magick->Scale( ... );

# ...and then save it.

However, if I manually set the image's color space to "RGB" right before I save it, the images are similar color-wise;

$magick->Colorspace( colorspace => 'RGB' );

Why is this?

EDIT: If I do exactly the same, except setting the color space manually, but convert to JPEG before saving, the colors becomes correct. Even more puzzling. :-/

Upvotes: 0

Views: 850

Answers (1)

Sinan Ünür
Sinan Ünür

Reputation: 118118

When the image is saved in PNG format using Image::Magick, a gAMA chunk is added as can be seen by comparing the output of gm identify -verbose modified.png with the output of gm identify -verbose original.png shows:

$ fc original.info modified.info
***** original.info
      Standard Deviation:      18869.16 (0.2879)
  Filesize: 613.0Ki
  Interlace: No
***** MODIFIED.INFO
      Standard Deviation:      18869.16 (0.2879)
  Gamma: 0.45455
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Filesize: 614.2Ki
  Interlace: No
*****

The RGB color values in the files are the same, but the saved gamma correction information in the second file causes it to be displayed slightly differently than the original. That is why converting the image to JPG "fixes" the problem: It removes the gamma correction information.

Looking at the ImageMagick source code, the stripping is achieved using:

SetImageArtifact(image,"png:exclude-chunk",
     "bKGD,cHRM,EXIF,gAMA,iCCP,iTXt,sRGB,tEXt,zCCP,zTXt,date");

Therefore, I recommended the OP try:

 $magick->Set(option => "png:exclude-chunk=gAMA");

in his Perl program, and the OP reported it solved the problem.

Related information:

Upvotes: 2

Related Questions