Improve heuristic used when saving progressive and optimized JPEGs with high quality values.

This commit is contained in:
Joaquín Cuenca Abela 2014-01-27 20:27:03 +01:00
parent e1489d7c76
commit bab068a337
2 changed files with 16 additions and 2 deletions

View File

@ -566,7 +566,10 @@ def _save(im, fp, filename):
# https://github.com/jdriscoll/django-imagekit/issues/50
bufsize=0
if "optimize" in info or "progressive" in info or "progression" in info:
bufsize = im.size[0]*im.size[1]
if quality >= 95:
bufsize = 2 * im.size[0] * im.size[1]
else:
bufsize = im.size[0] * im.size[1]
# The exif info needs to be written as one block, + APP1, + one spare byte.
# Ensure that our buffer is big enough

View File

@ -1,5 +1,8 @@
from tester import *
import array
import random
from PIL import Image
from PIL import ImageFile
@ -132,13 +135,21 @@ def test_progressive_large_buffer():
im = Image.new("RGB", (4096,4096), 0xff3333)
im.save(f, format="JPEG", progressive=True)
def test_progressive_large_buffer_highest_quality():
f = tempfile('temp.jpg')
a = array.array('B')
a.extend(random.randint(0, 255) for _ in xrange(256 * 256 * 3))
im = Image.frombuffer("RGB", (256, 256), a, "raw", "RGB", 0, 1)
# this requires more bytes than pixels in the image
im.save(f, format="JPEG", progressive=True, quality=100)
def test_large_exif():
#https://github.com/python-imaging/Pillow/issues/148
f = tempfile('temp.jpg')
im = lena()
im.save(f,'JPEG', quality=90, exif=b"1"*65532)
def test_progressive():
def test_progressive_compat():
im1 = roundtrip(lena())
im2 = roundtrip(lena(), progressive=1)
im3 = roundtrip(lena(), progression=1) # compatibility