GetPixel after SetPixel gives incorrect result

后端 未结 1 993
孤城傲影
孤城傲影 2021-01-26 20:17

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          


        
相关标签:
1条回答
  • 2021-01-26 20:25

    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

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

    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.

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

    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
    0 讨论(0)
提交回复
热议问题