MpoImagePlugin.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. exif = self.getexif()
  70. if 40962 in exif and 40963 in exif:
  71. self._size = (exif[40962], exif[40963])
  72. elif "exif" in self.info:
  73. del self.info["exif"]
  74. self.tile = [("jpeg", (0, 0) + self.size, self.offset, (self.mode, ""))]
  75. self.__frame = frame
  76. def tell(self):
  77. return self.__frame
  78. def _close__fp(self):
  79. try:
  80. if self.__fp != self.fp:
  81. self.__fp.close()
  82. except AttributeError:
  83. pass
  84. finally:
  85. self.__fp = None
  86. @staticmethod
  87. def adopt(jpeg_instance, mpheader=None):
  88. """
  89. Transform the instance of JpegImageFile into
  90. an instance of MpoImageFile.
  91. After the call, the JpegImageFile is extended
  92. to be an MpoImageFile.
  93. This is essentially useful when opening a JPEG
  94. file that reveals itself as an MPO, to avoid
  95. double call to _open.
  96. """
  97. jpeg_instance.__class__ = MpoImageFile
  98. jpeg_instance._after_jpeg_open(mpheader)
  99. return jpeg_instance
  100. # ---------------------------------------------------------------------
  101. # Registry stuff
  102. # Note that since MPO shares a factory with JPEG, we do not need to do a
  103. # separate registration for it here.
  104. # Image.register_open(MpoImageFile.format,
  105. # JpegImagePlugin.jpeg_factory, _accept)
  106. Image.register_save(MpoImageFile.format, _save)
  107. Image.register_extension(MpoImageFile.format, ".mpo")
  108. Image.register_mime(MpoImageFile.format, "image/mpo")