问题描述
我有一些JPEG实验,说100完全禁用JPEG量化阶段。
I have some experiments with JPEG, the doc said "100 completely disables the JPEG quantization stage."
然而,我在保存期间仍然进行了一些像素修改。这是我的代码:
However, I still got some pixel modification during saving. Here is my code:
import Image
red = [20,30,40,50,60,70];
img = Image.new("RGB", [1, len(red)], (255,255,255))
pix = img.load()
for x in range(0,len(red)):
pix[0,x] = (red[x],255,255)
img.save('test.jpg',quality=100)
img = Image.open('test.jpg')
pix = img.load()
for x in range(0,len(red)):
print pix[0,x][0],
我得到了意想不到的输出: 22 25 42 45 62 65
我应该怎么做才能保留像素值?请注意,我也使用尝试使用PHP,它给了我正确的价值当质量= 100时。
I got unexpected output: 22 25 42 45 62 65
What should I do to preserve the pixel value ? Please note that I also tried with PHP using imagejpeg and It gives me the correct value when quality=100.
我可以使用 png
来保存,但我想知道这背后的原因和如果有任何选择可以避免
I can use png
to preserve, but I want to know the reason behind this and if there is any option to avoid
推荐答案
JPEG由许多不同的步骤组成,其中许多步骤会引入一些损失。通过使用仅包含红色的样本图像,您可能遇到了最严重的罪犯 - 。一半的颜色信息被丢弃,因为眼睛对亮度变化比颜色变化更敏感。
JPEG consists of many different steps, many of which introduce some loss. By using a sample image containing only red, you've probably run across the worst offender - downsampling or chroma subsampling. Half of the color information is thrown away because the eye is more sensitive to brightness changes than color changes.
某些JPEG编码器可以配置为关闭子采样,但我不喜欢我认为PIL的情况就是如此。在任何情况下,它都不会给你一个完全无损的文件,即使你可以,因为还有其他步骤引入了损失。
Some JPEG encoders can be configured to turn off subsampling, but I don't think that's the case for PIL. In any case it won't give you a completely lossless file even if you could, since there are still other steps that introduce a loss.
这篇关于PIL jpeg,如何保留像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!