MpoImagePlugin.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # MPO file handling
  6. #
  7. # See "Multi-Picture Format" (CIPA DC-007-Translation 2009, Standard of the
  8. # Camera & Imaging Products Association)
  9. #
  10. # The multi-picture object combines multiple JPEG images (with a modified EXIF
  11. # data format) into a single file. While it can theoretically be used much like
  12. # a GIF animation, it is commonly used to represent 3D photographs and is (as
  13. # of this writing) the most commonly used format by 3D cameras.
  14. #
  15. # History:
  16. # 2014-03-13 Feneric Created
  17. #
  18. # See the README file for information on usage and redistribution.
  19. #
  20. from . import Image, ImageFile, JpegImagePlugin
  21. from ._binary import i16be as i16
  22. def _accept(prefix):
  23. return JpegImagePlugin._accept(prefix)
  24. def _save(im, fp, filename):
  25. # Note that we can only save the current frame at present
  26. return JpegImagePlugin._save(im, fp, filename)
  27. ##
  28. # Image plugin for MPO images.
  29. class MpoImageFile(JpegImagePlugin.JpegImageFile):
  30. format = "MPO"
  31. format_description = "MPO (CIPA DC-007)"
  32. _close_exclusive_fp_after_loading = False
  33. def _open(self):
  34. self.fp.seek(0) # prep the fp in order to pass the JPEG test
  35. JpegImagePlugin.JpegImageFile._open(self)
  36. self._after_jpeg_open()
  37. def _after_jpeg_open(self, mpheader=None):
  38. self.mpinfo = mpheader if mpheader is not None else self._getmp()
  39. self.n_frames = self.mpinfo[0xB001]
  40. self.__mpoffsets = [
  41. mpent["DataOffset"] + self.info["mpoffset"] for mpent in self.mpinfo[0xB002]
  42. ]
  43. self.__mpoffsets[0] = 0
  44. # Note that the following assertion will only be invalid if something
  45. # gets broken within JpegImagePlugin.
  46. assert self.n_frames == len(self.__mpoffsets)
  47. del self.info["mpoffset"] # no longer needed
  48. self.is_animated = self.n_frames > 1
  49. self.__fp = self.fp # FIXME: hack
  50. self.__fp.seek(self.__mpoffsets[0]) # get ready to read first frame
  51. self.__frame = 0
  52. self.offset = 0
  53. # for now we can only handle reading and individual frame extraction
  54. self.readonly = 1
  55. def load_seek(self, pos):
  56. self.__fp.seek(pos)
  57. def seek(self, frame):
  58. if not self._seek_check(frame):
  59. return
  60. self.fp = self.__fp
  61. self.offset = self.__mpoffsets[frame]
  62. self.fp.seek(self.offset + 2) # skip SOI marker
  63. segment = self.fp.read(2)
  64. if not segment:
  65. raise ValueError("No data found for frame")
  66. if i16(segment) == 0xFFE1: # APP1
  67. n = i16(self.fp.read(2)) - 2
  68. self.info["exif"] = ImageFile._safe_read(self.fp, n)
  69. mptype = self.mpinfo[0xB002][frame]["Attribute"]["MPType"]
  70. if mptype.startswith("Large Thumbnail"):
  71. exif = self.getexif().get_ifd(0x8769)
  72. if 40962 in exif and 40963 in exif:
  73. self._size = (exif[40962], exif[40963])
  74. elif "exif" in self.info:
  75. del self.info["exif"]
  76. self.tile = [("jpeg", (0, 0) + self.size, self.offset, (self.mode, ""))]
  77. self.__frame = frame
  78. def tell(self):
  79. return self.__frame
  80. def _close__fp(self):
  81. try:
  82. if self.__fp != self.fp:
  83. self.__fp.close()
  84. except AttributeError:
  85. pass
  86. finally:
  87. self.__fp = None
  88. @staticmethod
  89. def adopt(jpeg_instance, mpheader=None):
  90. """
  91. Transform the instance of JpegImageFile into
  92. an instance of MpoImageFile.
  93. After the call, the JpegImageFile is extended
  94. to be an MpoImageFile.
  95. This is essentially useful when opening a JPEG
  96. file that reveals itself as an MPO, to avoid
  97. double call to _open.
  98. """
  99. jpeg_instance.__class__ = MpoImageFile
  100. jpeg_instance._after_jpeg_open(mpheader)
  101. return jpeg_instance
  102. # ---------------------------------------------------------------------
  103. # Registry stuff
  104. # Note that since MPO shares a factory with JPEG, we do not need to do a
  105. # separate registration for it here.
  106. # Image.register_open(MpoImageFile.format,
  107. # JpegImagePlugin.jpeg_factory, _accept)
  108. Image.register_save(MpoImageFile.format, _save)
  109. Image.register_extension(MpoImageFile.format, ".mpo")
  110. Image.register_mime(MpoImageFile.format, "image/mpo")