From 7e2bd28a7cbc7fd7a01fc5bcef7956279ed99d85 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Sat, 3 Dec 2016 14:45:45 +0000 Subject: [PATCH] Fix for issue 2272, CMYK images miss the heuristic for the maxblock buffer scaling --- PIL/JpegImagePlugin.py | 5 ++++- Tests/test_file_jpeg.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index 221bf6495..4daf67aad 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -716,8 +716,11 @@ def _save(im, fp, filename): # https://github.com/matthewwithanm/django-imagekit/issues/50 bufsize = 0 if optimize or progressive: + # CMYK can be bigger + if im.mode == 'CMYK': + bufsize = 4 * im.size[0] * im.size[1] # keep sets quality to 0, but the actual value may be high. - if quality >= 95 or quality == 0: + elif quality >= 95 or quality == 0: bufsize = 2 * im.size[0] * im.size[1] else: bufsize = im.size[0] * im.size[1] diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 7cbb96c2e..b703598c5 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -171,6 +171,12 @@ class TestFileJpeg(PillowTestCase): # this requires more bytes than pixels in the image im.save(f, format="JPEG", progressive=True, quality=100) + def test_progressive_cmyk_buffer(self): + # Issue 2272, quality 90 cmyk image is tripping the large buffer bug. + f = BytesIO() + im = self.gen_random_image((256,256), 'CMYK') + im.save(f, format='JPEG', progressive=True, quality=94) + def test_large_exif(self): # https://github.com/python-pillow/Pillow/issues/148 f = self.tempfile('temp.jpg')