Merge pull request #504 from e98cuenc/highquality

Improve heuristic used when saving progressive and optimized JPEGs with high quality values.
This commit is contained in:
Alex Clark ☺ 2014-02-10 07:23:44 -05:00
commit cbdd639838
2 changed files with 17 additions and 2 deletions

View File

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

View File

@ -1,5 +1,7 @@
from tester import * from tester import *
import random
from PIL import Image from PIL import Image
from PIL import ImageFile from PIL import ImageFile
@ -132,13 +134,23 @@ def test_progressive_large_buffer():
im = Image.new("RGB", (4096,4096), 0xff3333) im = Image.new("RGB", (4096,4096), 0xff3333)
im.save(f, format="JPEG", progressive=True) im.save(f, format="JPEG", progressive=True)
def test_progressive_large_buffer_highest_quality():
f = tempfile('temp.jpg')
if py3:
a = bytes(random.randint(0, 255) for _ in range(256 * 256 * 3))
else:
a = b''.join(chr(random.randint(0, 255)) for _ in range(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(): def test_large_exif():
#https://github.com/python-imaging/Pillow/issues/148 #https://github.com/python-imaging/Pillow/issues/148
f = tempfile('temp.jpg') f = tempfile('temp.jpg')
im = lena() im = lena()
im.save(f,'JPEG', quality=90, exif=b"1"*65532) im.save(f,'JPEG', quality=90, exif=b"1"*65532)
def test_progressive(): def test_progressive_compat():
im1 = roundtrip(lena()) im1 = roundtrip(lena())
im2 = roundtrip(lena(), progressive=1) im2 = roundtrip(lena(), progressive=1)
im3 = roundtrip(lena(), progression=1) # compatibility im3 = roundtrip(lena(), progression=1) # compatibility