Merge pull request #2596 from Darou/master

Adjust buffer size when icc_profile > MAXBLOCK (issue #148)
This commit is contained in:
wiredfool 2017-06-29 13:18:29 +01:00 committed by GitHub
commit 9bc50d8f5c
3 changed files with 16 additions and 2 deletions

View File

@ -732,8 +732,9 @@ def _save(im, fp, filename):
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
bufsize = max(ImageFile.MAXBLOCK, bufsize, len(info.get("exif", b"")) + 5)
# Ensure that our buffer is big enough. Same with the icc_profile block.
bufsize = max(ImageFile.MAXBLOCK, bufsize, len(info.get("exif", b"")) + 5,
len(extra) + 1)
ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)

Binary file not shown.

After

Width:  |  Height:  |  Size: 500 KiB

View File

@ -133,6 +133,19 @@ class TestFileJpeg(PillowTestCase):
test(ImageFile.MAXBLOCK+1) # full buffer block plus one byte
test(ImageFile.MAXBLOCK*4+3) # large block
def test_large_icc_meta(self):
# https://github.com/python-pillow/Pillow/issues/148
# Sometimes the meta data on the icc_profile block is bigger than
# Image.MAXBLOCK or the image size.
im = Image.open('Tests/images/icc_profile_big.jpg')
f = self.tempfile("temp.jpg")
icc_profile = im.info["icc_profile"]
try:
im.save(f, format='JPEG', progressive=True,quality=95,
icc_profile=icc_profile, optimize=True)
except IOError:
self.fail("Failed saving image with icc larger than image size")
def test_optimize(self):
im1 = self.roundtrip(hopper())
im2 = self.roundtrip(hopper(), optimize=0)