You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.4 KiB

4 years ago
  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 . import Image, TiffImagePlugin
  19. import olefile
  20. __version__ = "0.1"
  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 IOError:
  37. raise SyntaxError("not an MIC file; invalid OLE file")
  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. raise SyntaxError("not an MIC file; no image entries")
  48. self.__fp = self.fp
  49. self.frame = None
  50. if len(self.images) > 1:
  51. self.category = Image.CONTAINER
  52. self.seek(0)
  53. @property
  54. def n_frames(self):
  55. return len(self.images)
  56. @property
  57. def is_animated(self):
  58. return len(self.images) > 1
  59. def seek(self, frame):
  60. if not self._seek_check(frame):
  61. return
  62. try:
  63. filename = self.images[frame]
  64. except IndexError:
  65. raise EOFError("no such frame")
  66. self.fp = self.ole.openstream(filename)
  67. TiffImagePlugin.TiffImageFile._open(self)
  68. self.frame = frame
  69. def tell(self):
  70. return self.frame
  71. #
  72. # --------------------------------------------------------------------
  73. Image.register_open(MicImageFile.format, MicImageFile, _accept)
  74. Image.register_extension(MicImageFile.format, ".mic")