Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In PIL (and mostly all softwares/librairies that use <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBgQFjAA&amp;url=http%3A%2F%2Fwww.ijg.org%2F&amp;ei=xIv6TNbMOIyt8Ab8o-zvCg&amp;usg=AFQjCNHslBwzbdmbbWUN8u4mM_MyAer7Cg&amp;sig2=dJt01vTnHP5te7Qlb2zK2w" rel="noreferrer">libjpeg</a>) the quality setting is use to construct the quantization table (<a href="http://refspecs.linuxfoundation.org/LSB_3.1.1/LSB-Desktop-generic/LSB-Desktop-generic/libjpeg.jpeg.set.linear.quality.1.html" rel="noreferrer">ref.</a>). 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).</p> <p>So, in others terms, the quality equal to the quantization table, so it's more complex then just a number.</p> <p>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. <code>cjpeg</code>, a command line utilities that come with libjpeg, can do that.</p> <p>Here's some rough code that save a jpeg with a specified quantization table:</p> <pre><code>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())) </code></pre> <p>You will need to find the way to extract the quantization table from the orginal jpeg. <code>djpeg</code> can do that (part of libjpeg):</p> <pre><code>djpeg -verbose -verbose image.jpg &gt; /dev/null </code></pre> <p>You will need also to find and set the sampling. For more info on that check <a href="http://en.wikipedia.org/wiki/JPEG#Color_space_transformation" rel="noreferrer">here</a>. You can also look at <a href="http://hg.effbot.org/pil-2009-raclette/src/e9ec832e1dd2/Tests/test_file_jpeg.py" rel="noreferrer"><code>test_subsampling</code></a></p> <p><strong>UPDATE</strong></p> <p>I did a PIL fork to add the possibility to specify subsampling or quantization tables or both when saving JPEG. You can also specify <code>quality='keep'</code> 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. <a href="https://bitbucket.org/etienned/pil-2009-raclette/" rel="noreferrer">My fork.</a></p> <p><strong>UPDATE 2</strong></p> <p>My code is now part of <a href="http://python-imaging.github.io/Pillow/" rel="noreferrer">Pillow 2.0</a>. So just do:</p> <pre><code>pip install Pillow </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload