MicImagePlugin.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # Microsoft Image Composer support for PIL
  6. #
  7. # Notes:
  8. # uses TiffImagePlugin.py to read the actual image streams
  9. #
  10. # History:
  11. # 97-01-20 fl Created
  12. #
  13. # Copyright (c) Secret Labs AB 1997.
  14. # Copyright (c) Fredrik Lundh 1997.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18. import olefile
  19. from . import Image, TiffImagePlugin
  20. #
  21. # --------------------------------------------------------------------
  22. def _accept(prefix):
  23. return prefix[:8] == olefile.MAGIC
  24. ##
  25. # Image plugin for Microsoft's Image Composer file format.
  26. class MicImageFile(TiffImagePlugin.TiffImageFile):
  27. format = "MIC"
  28. format_description = "Microsoft Image Composer"
  29. _close_exclusive_fp_after_loading = False
  30. def _open(self):
  31. # read the OLE directory and see if this is a likely
  32. # to be a Microsoft Image Composer file
  33. try:
  34. self.ole = olefile.OleFileIO(self.fp)
  35. except OSError as e:
  36. msg = "not an MIC file; invalid OLE file"
  37. raise SyntaxError(msg) from e
  38. # find ACI subfiles with Image members (maybe not the
  39. # best way to identify MIC files, but what the... ;-)
  40. self.images = []
  41. for path in self.ole.listdir():
  42. if path[1:] and path[0][-4:] == ".ACI" and path[1] == "Image":
  43. self.images.append(path)
  44. # if we didn't find any images, this is probably not
  45. # an MIC file.
  46. if not self.images:
  47. msg = "not an MIC file; no image entries"
  48. raise SyntaxError(msg)
  49. self.frame = None
  50. self._n_frames = len(self.images)
  51. self.is_animated = self._n_frames > 1
  52. self.seek(0)
  53. def seek(self, frame):
  54. if not self._seek_check(frame):
  55. return
  56. try:
  57. filename = self.images[frame]
  58. except IndexError as e:
  59. msg = "no such frame"
  60. raise EOFError(msg) from e
  61. self.fp = self.ole.openstream(filename)
  62. TiffImagePlugin.TiffImageFile._open(self)
  63. self.frame = frame
  64. def tell(self):
  65. return self.frame
  66. def close(self):
  67. self.ole.close()
  68. super().close()
  69. def __exit__(self, *args):
  70. self.ole.close()
  71. super().__exit__()
  72. #
  73. # --------------------------------------------------------------------
  74. Image.register_open(MicImageFile.format, MicImageFile, _accept)
  75. Image.register_extension(MicImageFile.format, ".mic")