MicImagePlugin.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.frame = None
  48. self._n_frames = len(self.images)
  49. self.is_animated = self._n_frames > 1
  50. if len(self.images) > 1:
  51. self._category = Image.CONTAINER
  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. raise EOFError("no such frame") from e
  60. self.fp = self.ole.openstream(filename)
  61. TiffImagePlugin.TiffImageFile._open(self)
  62. self.frame = frame
  63. def tell(self):
  64. return self.frame
  65. #
  66. # --------------------------------------------------------------------
  67. Image.register_open(MicImageFile.format, MicImageFile, _accept)
  68. Image.register_extension(MicImageFile.format, ".mic")