Avoid potential draft artifacts by resizing

This commit is contained in:
Andrew Murray 2019-01-09 17:35:59 +11:00
parent 7bf5246b93
commit daad220b55
2 changed files with 19 additions and 0 deletions

View File

@ -581,6 +581,11 @@ class TestFileJpeg(PillowTestCase):
# OSError for unidentified image.
self.assertEqual(im.info.get("dpi"), (72, 72))
def test_partial_mcu(self):
im = Image.open("Tests/images/flower2.jpg")
im.draft(im.mode, (100, 100))
self.assertEqual(im.size, (100, 100))
@unittest.skipUnless(sys.platform.startswith('win32'), "Windows only")
class TestFileCloseW32(PillowTestCase):

View File

@ -396,6 +396,20 @@ class JpegImageFile(ImageFile.ImageFile):
self.tile = [(d, e, o, a)]
self.decoderconfig = (scale, 0)
if scale > 1:
sampling = get_sampling(self)
if sampling != -1:
mcu = [(8, 8), (16, 8), (16, 16)][sampling]
for i in range(0, 2):
# If an original image dimension is not a whole number of
# MCUs, then the additional data may not be correct.
if self.size[i] % mcu[i] != 0:
im = self.resize(size)
self.im = im.im
self._size = size
break
return self
def load_djpeg(self):