MicImagePlugin.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. from __future__ import annotations
  19. import olefile
  20. from . import Image, TiffImagePlugin
  21. #
  22. # --------------------------------------------------------------------
  23. def _accept(prefix):
  24. return prefix[:8] == olefile.MAGIC
  25. ##
  26. # Image plugin for Microsoft's Image Composer file format.
  27. class MicImageFile(TiffImagePlugin.TiffImageFile):
  28. format = "MIC"
  29. format_description = "Microsoft Image Composer"
  30. _close_exclusive_fp_after_loading = False
  31. def _open(self):
  32. # read the OLE directory and see if this is a likely
  33. # to be a Microsoft Image Composer file
  34. try:
  35. self.ole = olefile.OleFileIO(self.fp)
  36. except OSError as e:
  37. msg = "not an MIC file; invalid OLE file"
  38. raise SyntaxError(msg) from e
  39. # find ACI subfiles with Image members (maybe not the
  40. # best way to identify MIC files, but what the... ;-)
  41. self.images = [
  42. path
  43. for path in self.ole.listdir()
  44. if path[1:] and path[0][-4:] == ".ACI" and path[1] == "Image"
  45. ]
  46. # if we didn't find any images, this is probably not
  47. # an MIC file.
  48. if not self.images:
  49. msg = "not an MIC file; no image entries"
  50. raise SyntaxError(msg)
  51. self.frame = None
  52. self._n_frames = len(self.images)
  53. self.is_animated = self._n_frames > 1
  54. self.__fp = self.fp
  55. self.seek(0)
  56. def seek(self, frame):
  57. if not self._seek_check(frame):
  58. return
  59. try:
  60. filename = self.images[frame]
  61. except IndexError as e:
  62. msg = "no such frame"
  63. raise EOFError(msg) from e
  64. self.fp = self.ole.openstream(filename)
  65. TiffImagePlugin.TiffImageFile._open(self)
  66. self.frame = frame
  67. def tell(self):
  68. return self.frame
  69. def close(self):
  70. self.__fp.close()
  71. self.ole.close()
  72. super().close()
  73. def __exit__(self, *args):
  74. self.__fp.close()
  75. self.ole.close()
  76. super().__exit__()
  77. #
  78. # --------------------------------------------------------------------
  79. Image.register_open(MicImageFile.format, MicImageFile, _accept)
  80. Image.register_extension(MicImageFile.format, ".mic")