Perl & Image::Magick, getting color values by pixel

蓝咒 提交于 2019-12-04 04:53:45

问题


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'?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3681415/perl-imagemagick-getting-color-values-by-pixel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!