MpoImagePlugin.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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._initial_size = self.size
  39. self.mpinfo = mpheader if mpheader is not None else self._getmp()
  40. self.n_frames = self.mpinfo[0xB001]
  41. self.__mpoffsets = [
  42. mpent["DataOffset"] + self.info["mpoffset"] for mpent in self.mpinfo[0xB002]
  43. ]
  44. self.__mpoffsets[0] = 0
  45. # Note that the following assertion will only be invalid if something
  46. # gets broken within JpegImagePlugin.
  47. assert self.n_frames == len(self.__mpoffsets)
  48. del self.info["mpoffset"] # no longer needed
  49. self.is_animated = self.n_frames > 1
  50. self._fp = self.fp # FIXME: hack
  51. self._fp.seek(self.__mpoffsets[0]) # get ready to read first frame
  52. self.__frame = 0
  53. self.offset = 0
  54. # for now we can only handle reading and individual frame extraction
  55. self.readonly = 1
  56. def load_seek(self, pos):
  57. self._fp.seek(pos)
  58. def seek(self, frame):
  59. if not self._seek_check(frame):
  60. return
  61. self.fp = self._fp
  62. self.offset = self.__mpoffsets[frame]
  63. self.fp.seek(self.offset + 2) # skip SOI marker
  64. segment = self.fp.read(2)
  65. if not segment:
  66. raise ValueError("No data found for frame")
  67. self._size = self._initial_size
  68. if i16(segment) == 0xFFE1: # APP1
  69. n = i16(self.fp.read(2)) - 2
  70. self.info["exif"] = ImageFile._safe_read(self.fp, n)
  71. self._reload_exif()
  72. mptype = self.mpinfo[0xB002][frame]["Attribute"]["MPType"]
  73. if mptype.startswith("Large Thumbnail"):
  74. exif = self.getexif().get_ifd(0x8769)
  75. if 40962 in exif and 40963 in exif:
  76. self._size = (exif[40962], exif[40963])
  77. elif "exif" in self.info:
  78. del self.info["exif"]
  79. self._reload_exif()
  80. self.tile = [("jpeg", (0, 0) + self.size, self.offset, (self.mode, ""))]
  81. self.__frame = frame
  82. def tell(self):
  83. return self.__frame
  84. @staticmethod
  85. def adopt(jpeg_instance, mpheader=None):
  86. """
  87. Transform the instance of JpegImageFile into
  88. an instance of MpoImageFile.
  89. After the call, the JpegImageFile is extended
  90. to be an MpoImageFile.
  91. This is essentially useful when opening a JPEG
  92. file that reveals itself as an MPO, to avoid
  93. double call to _open.
  94. """
  95. jpeg_instance.__class__ = MpoImageFile
  96. jpeg_instance._after_jpeg_open(mpheader)
  97. return jpeg_instance
  98. # ---------------------------------------------------------------------
  99. # Registry stuff
  100. # Note that since MPO shares a factory with JPEG, we do not need to do a
  101. # separate registration for it here.
  102. # Image.register_open(MpoImageFile.format,
  103. # JpegImagePlugin.jpeg_factory, _accept)
  104. Image.register_save(MpoImageFile.format, _save)
  105. Image.register_extension(MpoImageFile.format, ".mpo")
  106. Image.register_mime(MpoImageFile.format, "image/mpo")