MicImagePlugin.py 2.5 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. 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. raise SyntaxError("not an MIC file; invalid OLE file") from e
  37. # find ACI subfiles with Image members (maybe not the
  38. # best way to identify MIC files, but what the... ;-)
  39. self.images = []
  40. for path in self.ole.listdir():
  41. if path[1:] and path[0][-4:] == ".ACI" and path[1] == "Image":
  42. self.images.append(path)
  43. # if we didn't find any images, this is probably not
  44. # an MIC file.
  45. if not self.images:
  46. raise SyntaxError("not an MIC file; no image entries")
  47. self.__fp = self.fp
  48. self.frame = None
  49. self._n_frames = len(self.images)
  50. self.is_animated = self._n_frames > 1
  51. if len(self.images) > 1:
  52. self._category = Image.CONTAINER
  53. self.seek(0)
  54. def seek(self, frame):
  55. if not self._seek_check(frame):
  56. return
  57. try:
  58. filename = self.images[frame]
  59. except IndexError as e:
  60. raise EOFError("no such frame") 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__fp(self):
  67. try:
  68. if self.__fp != self.fp:
  69. self.__fp.close()
  70. except AttributeError:
  71. pass
  72. finally:
  73. self.__fp = None
  74. #
  75. # --------------------------------------------------------------------
  76. Image.register_open(MicImageFile.format, MicImageFile, _accept)
  77. Image.register_extension(MicImageFile.format, ".mic")