Minor refactoring per discussion and MPO docs.

This commit is contained in:
Eric W. Brown 2014-07-24 11:16:12 -04:00
parent 8d7266afc5
commit dcd171c1b8
4 changed files with 34 additions and 15 deletions

View File

@ -2236,19 +2236,6 @@ def open(fp, mode="r"):
fp.seek(0)
im = factory(fp, filename)
_decompression_bomb_check(im.size)
if i == 'JPEG':
# Things are more complicated for JPEGs; we need to parse
# more deeply than 16 characters and check the contents of
# a potential MP header block to be sure.
mpheader = im._getmp()
try:
if mpheader[45057] > 1:
# It's actually an MPO
factory, accept = OPEN['MPO']
im = factory(fp, filename)
except (TypeError, IndexError):
# It is really a JPEG
pass
return im
except (SyntaxError, IndexError, TypeError):
# import traceback

View File

@ -663,10 +663,27 @@ def _save_cjpeg(im, fp, filename):
except:
pass
##
# Factory for making JPEG and MPO instances
def jpeg_factory(fp=None, filename=None):
im = JpegImageFile(fp, filename)
mpheader = im._getmp()
try:
if mpheader[45057] > 1:
# It's actually an MPO
from MpoImagePlugin import MpoImageFile
im = MpoImageFile(fp, filename)
except (TypeError, IndexError):
# It is really a JPEG
pass
return im
# -------------------------------------------------------------------q-
# Registry stuff
Image.register_open("JPEG", JpegImageFile, _accept)
Image.register_open("JPEG", jpeg_factory, _accept)
Image.register_save("JPEG", _save)
Image.register_extension("JPEG", ".jfif")

View File

@ -37,6 +37,7 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
format_description = "MPO (CIPA DC-007)"
def _open(self):
self.fp.seek(0) # prep the fp in order to pass the JPEG test
JpegImagePlugin.JpegImageFile._open(self)
self.mpinfo = self._getmp()
self.__framecount = self.mpinfo[0xB001]
@ -73,7 +74,7 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
# -------------------------------------------------------------------q-
# Registry stuff
Image.register_open("MPO", MpoImageFile, _accept)
Image.register_open("MPO", JpegImagePlugin.jpeg_factory, _accept)
Image.register_save("MPO", _save)
Image.register_extension("MPO", ".mpo")

View File

@ -588,6 +588,20 @@ PIL identifies and reads Microsoft Image Composer (MIC) files. When opened, the
first sprite in the file is loaded. You can use :py:meth:`~file.seek` and
:py:meth:`~file.tell` to read other sprites from the file.
MPO
^^^
PIL identifies and reads Multi Picture Object (MPO) files, loading the primary
image when first opened. The :py:meth:`~file.seek` and :py:meth:`~file.tell`
methods may be used to read other pictures from the file. The pictures are
zero-indexed and random access is supported.
MIC (read only)
PIL identifies and reads Microsoft Image Composer (MIC) files. When opened, the
first sprite in the file is loaded. You can use :py:meth:`~file.seek` and
:py:meth:`~file.tell` to read other sprites from the file.
PCD
^^^