ImtImagePlugin.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # IM Tools support for PIL
  6. #
  7. # history:
  8. # 1996-05-27 fl Created (read 8-bit images only)
  9. # 2001-02-17 fl Use 're' instead of 'regex' (Python 2.1) (0.2)
  10. #
  11. # Copyright (c) Secret Labs AB 1997-2001.
  12. # Copyright (c) Fredrik Lundh 1996-2001.
  13. #
  14. # See the README file for information on usage and redistribution.
  15. #
  16. import re
  17. from . import Image, ImageFile
  18. #
  19. # --------------------------------------------------------------------
  20. field = re.compile(rb"([a-z]*) ([^ \r\n]*)")
  21. ##
  22. # Image plugin for IM Tools images.
  23. class ImtImageFile(ImageFile.ImageFile):
  24. format = "IMT"
  25. format_description = "IM Tools"
  26. def _open(self):
  27. # Quick rejection: if there's not a LF among the first
  28. # 100 bytes, this is (probably) not a text header.
  29. buffer = self.fp.read(100)
  30. if b"\n" not in buffer:
  31. msg = "not an IM file"
  32. raise SyntaxError(msg)
  33. xsize = ysize = 0
  34. while True:
  35. if buffer:
  36. s = buffer[:1]
  37. buffer = buffer[1:]
  38. else:
  39. s = self.fp.read(1)
  40. if not s:
  41. break
  42. if s == b"\x0C":
  43. # image data begins
  44. self.tile = [
  45. (
  46. "raw",
  47. (0, 0) + self.size,
  48. self.fp.tell() - len(buffer),
  49. (self.mode, 0, 1),
  50. )
  51. ]
  52. break
  53. else:
  54. # read key/value pair
  55. if b"\n" not in buffer:
  56. buffer += self.fp.read(100)
  57. lines = buffer.split(b"\n")
  58. s += lines.pop(0)
  59. buffer = b"\n".join(lines)
  60. if len(s) == 1 or len(s) > 100:
  61. break
  62. if s[0] == ord(b"*"):
  63. continue # comment
  64. m = field.match(s)
  65. if not m:
  66. break
  67. k, v = m.group(1, 2)
  68. if k == b"width":
  69. xsize = int(v)
  70. self._size = xsize, ysize
  71. elif k == b"height":
  72. ysize = int(v)
  73. self._size = xsize, ysize
  74. elif k == b"pixel" and v == b"n8":
  75. self._mode = "L"
  76. #
  77. # --------------------------------------------------------------------
  78. Image.register_open(ImtImageFile.format, ImtImageFile)
  79. #
  80. # no extension registered (".im" is simply too common)