Treat MPO with unknown header as base JPEG file

This commit is contained in:
Andrew Murray 2015-07-19 22:56:04 +10:00
parent df02be0c70
commit 2a3a34d2ff
3 changed files with 17 additions and 1 deletions

View File

@ -37,6 +37,7 @@ __version__ = "0.6"
import array
import struct
import io
import warnings
from struct import unpack
from PIL import Image, ImageFile, TiffImagePlugin, _binary
from PIL.JpegPresets import presets
@ -713,8 +714,8 @@ def _save_cjpeg(im, fp, filename):
# Factory for making JPEG and MPO instances
def jpeg_factory(fp=None, filename=None):
im = JpegImageFile(fp, filename)
mpheader = im._getmp()
try:
mpheader = im._getmp()
if mpheader[45057] > 1:
# It's actually an MPO
from .MpoImagePlugin import MpoImageFile
@ -722,6 +723,10 @@ def jpeg_factory(fp=None, filename=None):
except (TypeError, IndexError):
# It is really a JPEG
pass
except SyntaxError:
warnings.warn("Image appears to be a malformed MPO file, it will be "
"interpreted as a base JPEG file")
pass
return im

Binary file not shown.

After

Width:  |  Height:  |  Size: 117 KiB

View File

@ -353,6 +353,17 @@ class TestFileJpeg(PillowTestCase):
reloaded.save(f, quality='keep', progressive=True)
reloaded.save(f, quality='keep', optimize=True)
def test_bad_mpo_header(self):
""" Treat unknown MPO as JPEG """
# Arrange
# Act
# Shouldn't raise error
im = Image.open("Tests/images/sugarshack_bad_mpo_header.jpg")
# Assert
self.assertEqual(im.format, "JPEG")
if __name__ == '__main__':
unittest.main()