PIL changes pixel value when saving

后端 未结 2 1541
灰色年华
灰色年华 2020-12-11 18:42

This is as simple as it gets. Make RGB image, one pixel big. Set pixel value to (a,b,c). Save. Problem is, the saved image has a different pixel value then (a,b,c). Usu

相关标签:
2条回答
  • 2020-12-11 18:47

    Save it as a .png. .jpg is a lossy compressed format.

    0 讨论(0)
  • 2020-12-11 19:07

    JPG doesn't promise to store precisely the pixels you intended. It compresses your data to make the file smaller, and the compression is based on human perception. The idea is to create an array of pixels that appear the same to a human, even though they are different pixels.

    So the pixel you are writing is in the output, but the adjacent pixels have been altered to be able to store the entire image in less space. This is called "lossy compression" because data is lost.

    Other image formats don't have this property. PNG is a lossless compressed format, meaning precisely the same pixels will results after decompressing a compressed PNG. If you save your image as a PNG, you will have the result you want.

    0 讨论(0)
提交回复
热议问题