Adjust buffer size when quality=keep, fixes #148 (again)

This commit is contained in:
wiredfool 2015-01-18 10:56:29 -08:00
parent fbc33970dd
commit 1e040feb1b
2 changed files with 21 additions and 1 deletions

View File

@ -684,7 +684,8 @@ 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:
if quality >= 95: # keep sets quality to 0, but the actual value may be high.
if quality >= 95 or quality == 0:
bufsize = 2 * im.size[0] * im.size[1] bufsize = 2 * im.size[0] * im.size[1]
else: else:
bufsize = im.size[0] * im.size[1] bufsize = im.size[0] * im.size[1]

View File

@ -3,6 +3,7 @@ from helper import djpeg_available, cjpeg_available
import random import random
from io import BytesIO from io import BytesIO
import os
from PIL import Image from PIL import Image
from PIL import ImageFile from PIL import ImageFile
@ -334,6 +335,24 @@ class TestFileJpeg(PillowTestCase):
self.assertEqual(tag_ids['RelatedImageWidth'], 0x1001) self.assertEqual(tag_ids['RelatedImageWidth'], 0x1001)
self.assertEqual(tag_ids['RelatedImageLength'], 0x1002) self.assertEqual(tag_ids['RelatedImageLength'], 0x1002)
def test_MAXBLOCK_scaling(self):
def gen_random_image(size):
""" Generates a very hard to compress file
:param size: tuple
"""
return Image.frombytes('RGB',size, os.urandom(size[0]*size[1] *3))
im = gen_random_image((512,512))
f = self.tempfile("temp.jpeg")
im.save(f, quality=100, optimize=True)
reloaded = Image.open(f)
# none of these should crash
reloaded.save(f, quality='keep')
reloaded.save(f, quality='keep', progressive=True)
reloaded.save(f, quality='keep', optimize=True)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()