MicImagePlugin.py 2.7 KB

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