views:

143

answers:

2

I'm using Perl and the Image::Magick module to process some JPEGs.

I'm using the GetPixels sub to get the RGB components of each pixel.

e.g.

my @pixels = $img->GetPixels(
    width     => 1,
    height    => 1,
    x         => 0,
    y         => 0,
    map       => 'RGB',
    #normalize => 1
)

print Dumper \@pixels;

$img->Resize(
    width  => 1,
    height => 1,
    filter => 'Lanczos'
);

@pixels = $img->GetPixels(
    width     => 1,
    height    => 1,
    x         => 0,
    y         => 0,
    map       => 'RGB',
    #normalize => 1
);

print Dumper \@pixels;

$img->Write('verify.jpg');

I've found that getPixels is returning two bytes per channel, e.g.

$VAR1 = [
          46260,
          45232,
          44975
        ];

$VAR1 = [
          58271,
          58949,
          60330
        ];

Before the call to Resize: (in this example) the colour of the designated pixel is #b4b0af, and the returned values are 0xB4B4, 0xB0B0, 0xAFAF. I don't understand why this is, but I can deal with it using MOD 256;

But after the call to Resize, the returned values don't correspond in any obvious way to the actual values I find in the output file (verify.jpg).

Is Image::Magick just being super-precise (accounting for the shorts instead of bytes)?
And does the JPEG compression account for the discrepancy between the second Dumper output and the contents of 'verify.jpg'?

A: 

JPEG compression is lossy, so there's no direct correspondence between the pixel values before saving and the pixels in the compressed image. You'd have to load the new image if you want to find out how the compression modified it.

cjm
+1  A: 

Read all about colors in ImageMagick, including its quantum depth:

ImageMagick may be compiled to support 32 or 64 bit pixels of type PixelPacket. This is controlled by the value of the QuantumDepth define. The default is 64 bit pixels, which provides the best accuracy.

You might also like to read about how it does color reduction.

brian d foy
Thanks brian. Very interesting links too. I think I'll use the higher precision, but out of interest, can I assume that right shifting the values above by 8 bits will give me an accurate three byte representation of the color?
aidan