From dcd171c1b8485eb4234344194727e55ea090b85f Mon Sep 17 00:00:00 2001 From: "Eric W. Brown" Date: Thu, 24 Jul 2014 11:16:12 -0400 Subject: [PATCH] Minor refactoring per discussion and MPO docs. --- PIL/Image.py | 13 ------------- PIL/JpegImagePlugin.py | 19 ++++++++++++++++++- PIL/MpoImagePlugin.py | 3 ++- docs/handbook/image-file-formats.rst | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index eb08fab9e..3a42e308b 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -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 diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index bedc42373..fbb855362 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -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") diff --git a/PIL/MpoImagePlugin.py b/PIL/MpoImagePlugin.py index 6dda2e513..3c5bc35d4 100644 --- a/PIL/MpoImagePlugin.py +++ b/PIL/MpoImagePlugin.py @@ -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") diff --git a/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index 03e55f35a..bfc94058a 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -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 ^^^