PIL jpeg,如何保留像素颜色 [英] PIL jpeg, how to preserve the pixel color

查看:152
本文介绍了PIL jpeg,如何保留像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些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
我应该怎么做才能保留像素值?请注意,我也使用 imagejpeg 尝试使用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,如何保留像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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