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.

95 lines
2.2 KiB

4 years ago
  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. __version__ = "0.2"
  19. #
  20. # --------------------------------------------------------------------
  21. field = re.compile(br"([a-z]*) ([^ \r\n]*)")
  22. ##
  23. # Image plugin for IM Tools images.
  24. class ImtImageFile(ImageFile.ImageFile):
  25. format = "IMT"
  26. format_description = "IM Tools"
  27. def _open(self):
  28. # Quick rejection: if there's not a LF among the first
  29. # 100 bytes, this is (probably) not a text header.
  30. if b"\n" not in self.fp.read(100):
  31. raise SyntaxError("not an IM file")
  32. self.fp.seek(0)
  33. xsize = ysize = 0
  34. while True:
  35. s = self.fp.read(1)
  36. if not s:
  37. break
  38. if s == b'\x0C':
  39. # image data begins
  40. self.tile = [("raw", (0, 0)+self.size,
  41. self.fp.tell(),
  42. (self.mode, 0, 1))]
  43. break
  44. else:
  45. # read key/value pair
  46. # FIXME: dangerous, may read whole file
  47. s = s + self.fp.readline()
  48. if len(s) == 1 or len(s) > 100:
  49. break
  50. if s[0] == ord(b"*"):
  51. continue # comment
  52. m = field.match(s)
  53. if not m:
  54. break
  55. k, v = m.group(1, 2)
  56. if k == "width":
  57. xsize = int(v)
  58. self._size = xsize, ysize
  59. elif k == "height":
  60. ysize = int(v)
  61. self._size = xsize, ysize
  62. elif k == "pixel" and v == "n8":
  63. self.mode = "L"
  64. #
  65. # --------------------------------------------------------------------
  66. Image.register_open(ImtImageFile.format, ImtImageFile)
  67. #
  68. # no extension registered (".im" is simply too common)