Merge pull request #1350 from radarhere/1328_mpo_fix

Treat MPO with unknown header as base JPEG file
This commit is contained in:
Hugo van Kemenade 2015-07-19 20:24:30 +03:00
commit e1fb53f83f
3 changed files with 17 additions and 1 deletions

View File

@ -37,6 +37,7 @@ __version__ = "0.6"
import array import array
import struct import struct
import io import io
import warnings
from struct import unpack from struct import unpack
from PIL import Image, ImageFile, TiffImagePlugin, _binary from PIL import Image, ImageFile, TiffImagePlugin, _binary
from PIL.JpegPresets import presets from PIL.JpegPresets import presets
@ -713,8 +714,8 @@ def _save_cjpeg(im, fp, filename):
# Factory for making JPEG and MPO instances # Factory for making JPEG and MPO instances
def jpeg_factory(fp=None, filename=None): def jpeg_factory(fp=None, filename=None):
im = JpegImageFile(fp, filename) im = JpegImageFile(fp, filename)
mpheader = im._getmp()
try: try:
mpheader = im._getmp()
if mpheader[45057] > 1: if mpheader[45057] > 1:
# It's actually an MPO # It's actually an MPO
from .MpoImagePlugin import MpoImageFile from .MpoImagePlugin import MpoImageFile
@ -722,6 +723,10 @@ def jpeg_factory(fp=None, filename=None):
except (TypeError, IndexError): except (TypeError, IndexError):
# It is really a JPEG # It is really a JPEG
pass 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 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', progressive=True)
reloaded.save(f, quality='keep', optimize=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__': if __name__ == '__main__':
unittest.main() unittest.main()