SetPixel之后的GetPixel会给出错误的结果 [英] GetPixel after SetPixel gives incorrect result

查看:140
本文介绍了SetPixel之后的GetPixel会给出错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个jpg设置像素。保存另一个jpg文件。再次阅读新文件。获取像素。但它给了我不正确的结果。
这是我的代码:

I set pixel from one jpg. Save it another jpg file. Read a new file again. Get pixel. But it gives me incorrect result. That's my code:

use Image::Magick;
use Data::Dumper;

my $im = new Image::Magick;
$im->Read('file1.jpg');
my @pixel = $im->GetPixel(x=>0,y=>0,channel=>'RGB', normalize=>'True');
print Dumper(@pixel);

my @color = ('1.0', '0.0', '0.0');
$im->SetPixel(x=>0, y=>0, channel=>'RGB', normalize=>'True', color => \@color);
$im->Write('file30.jpg');

@pixel = $im->GetPixel(x=>0,y=>0,channel=>'RGB', normalize=>'True');
print Dumper(@pixel);

print "-" x 30, "\n";
my $nim = new Image::Magick;
$nim->Read('file30.jpg');

my @npixel = $nim->GetPixel(x=>0,y=>0,channel=>'RGB', normalize=>'True');
print Dumper(@npixel);

当我运行它时:

$VAR1 = '0.133333333333333';
$VAR2 = '0.141176470588235';
$VAR3 = '0.0588235294117647';
$VAR1 = '1';
$VAR2 = '0';
$VAR3 = '0';
------------------------------
$VAR1 = '0.32156862745098';
$VAR2 = '0.247058823529412';
$VAR3 = '0.188235294117647';

为什么@npixel会给我错误的结果?如何解决?

Why @npixel gives me wrong result? How can it be fixed?

UPD

它适用于.BMP文件。

It works fine with .BMP files.

推荐答案

发布对应我上述评论的答案。

Posting an answer to correspond to my comment above.

最好通过有损压缩与无损压缩的比较来解释:

This would be best explained by a comparison of lossy vs lossless compression:

https://en.wikipedia.org/wiki/Lossy_compression

简而言之,您可以使用RGB像素数组使用imagemagick,设置像素值,获取它们等等。

In short, you can work with an array of RGB pixels with imagemagick, set pixel values, get them, etc.

保存时,还有其他操作无法控制。对于JPEG,它是一种称为DCT(离散余弦变换)的有损压缩算法: http:// en.wikipedia.org/wiki/Discrete_cosine_transform 。为了减小文件大小,这种有损压缩(在JPEG的情况下)是必要的。

When you save, there are additional operations that are out of your control. In the case of JPEG, it is a lossy compression algorithm known as DCT (discrete cosine transformation): http://en.wikipedia.org/wiki/Discrete_cosine_transform. This lossy compression (in the case of JPEG) is necessary in order to reduce the file size.

如果你不想遇到这个问题,你需要工作with:

If you do not want to encounter this issue, you either need to work with:


  1. 未压缩数据(即:RAW / BMP文件)。保存文件时,它将按原样写入输出文件。不施加压缩或扭曲。 http://en.wikipedia.org/wiki/BMP_file_format

  2. 使用无损压缩。这通常会压缩数据以减小文件大小,但不会像有损压缩那样缩小文件大小。 PNG就是一个例子,ImageMagick支持它。您的数据不会按原样写入输出文件,但在保存和打开时会将其翻译,因此您可以准确地获取每个像素的所有原始数据。
    http://en.wikipedia.org/wiki/Portable_Network_Graphics

  1. Uncompressed data (ie: RAW/BMP files). When you save the file, it is written as-is to an output file. No compression or distortion is applied. http://en.wikipedia.org/wiki/BMP_file_format
  2. Use lossless compression. This typically compresses the data to reduce the file size, but does not shrink the file size as much as lossy compression does. PNG is an example of this, and ImageMagick supports it. Your data is NOT written as-is to the output file, but it is translated when saving and opening so you get all the original data back for every pixel exactly. http://en.wikipedia.org/wiki/Portable_Network_Graphics

这篇关于SetPixel之后的GetPixel会给出错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆