用Python确定JPG质量(PIL) [英] Determining JPG quality in Python (PIL)

查看:179
本文介绍了用Python确定JPG质量(PIL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩Python中的PIL库,我想知道如何确定给定JPG图像的质量。我尝试打开JPG图像做一些事情并再次保存它的原始质量。 Image.save让我确定所需的质量:

I am playing around with PIL library in Python and I am wondering how do I determine quality of given JPG image. I try to open JPG image do something to it and save it again in the its original quality. Image.save let me determine the desired quality:

im.save(name, quality = x)  

但是我看不出任何提取原始文件的方法。现在我只是猜测并尝试通过对'质量'参数进行二进制搜索来获得与输入大小相同的输出文件,但这是不可接受的长期解决方案:)

我也尝试过使用: Image.info但我的大部分图片都没有任何有用的信息(例如:'adobe','icc_profile','exif','adobe_transform')

帮助!

but I can't see any way to extract original one. For now I am just guessing and try to have an output file of the same size as input by doing binary search on 'quality' parameter but this is not acceptable long term solution :)
I also tried using: Image.info but most of my images don't have any useful information there (ex: 'adobe', 'icc_profile', 'exif', 'adobe_transform')
Help !

推荐答案

在PIL中(以及大多数使用的libjpeg )质量设置用于构建量化表( ref。)。在libjpeg中,质量数缩放样本表值(来自JPEG规范第K.1节)。在其他图书馆中,有不同的表分配不同的质量(例如:Photoshop,数码相机)。

In PIL (and mostly all softwares/librairies that use libjpeg) the quality setting is use to construct the quantization table (ref.). In libjpeg the quality number "scale" the sample table values (from the JPEG specification Section K.1). In other librairies there's different tables assign to different qualities (ex.: Photoshop, digital camera).

所以,在其他方面,质量等于量化表,所以它比一个数字更复杂。

So, in others terms, the quality equal to the quantization table, so it's more complex then just a number.

如果你想用相同的质量保存你的修改图像,你只需要使用相同的量化表。幸运的是,量化表嵌入在每个JPEG中。不幸的是,在PIL中保存时无法指定量化表。 cjpeg ,libjpeg附带的命令行实用程序,可以做到这一点。

If you want to save your modify images with the same "quality", you only need to use the same quantization table. Fortunately, the quantization table is embeded in each JPEG. Unfortunately, it's not possible to specify a quantization table when saving in PIL. cjpeg, a command line utilities that come with libjpeg, can do that.

这里有一些粗略的代码可以保存具有指定量化表的jpeg:

Here's some rough code that save a jpeg with a specified quantization table:

from subprocess import Popen, PIPE
from PIL import Image, ImageFilter

proc = Popen('%s -sample 1x1 -optimize -progressive -qtables %s -outfile %s' % ('path/to/cjpeg', '/path/ta/qtable', 'out.jpg'), shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
P = '6'
if im.mode == 'L':
    P = '5'
stdout, stderr = proc.communicate('P%s\n%s %s\n255\n%s' % (P, im.size[0], im.size[1], im.tostring()))

您需要找到提取量化表的方法来自原始的jpeg。 djpeg 可以做到(libjpeg的一部分):

You will need to find the way to extract the quantization table from the orginal jpeg. djpeg can do that (part of libjpeg):

djpeg -verbose -verbose image.jpg > /dev/null

您还需要查找和设置采样。有关该检查的更多信息,请此处。您还可以查看 test_subsampling

You will need also to find and set the sampling. For more info on that check here. You can also look at test_subsampling

更新

我做了一个PIL分支,可以在保存JPEG时添加指定子采样或量化表或两者的可能性。您还可以在保存时指定 quality ='keep',并使用与原始图像相同的量化表和子采样保存图像(原始图像需要为JPEG)。还有一些预设(基于Photoshop),您可以在保存时传递质量。 我的前叉。

I did a PIL fork to add the possibility to specify subsampling or quantization tables or both when saving JPEG. You can also specify quality='keep' when saving and the image will be saved with the same quantization tables and subsampling as the original (original need to be a JPEG). There's also some presets (based on Photoshop) that you can pass to quality when saving. My fork.

更新2

我的代码现在是 Pillow 2.0 。所以这样做:

My code is now part of Pillow 2.0. So just do:

pip install Pillow

这篇关于用Python确定JPG质量(PIL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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