ImtImagePlugin.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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(br"([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. if b"\n" not in self.fp.read(100):
  30. raise SyntaxError("not an IM file")
  31. self.fp.seek(0)
  32. xsize = ysize = 0
  33. while True:
  34. s = self.fp.read(1)
  35. if not s:
  36. break
  37. if s == b"\x0C":
  38. # image data begins
  39. self.tile = [
  40. ("raw", (0, 0) + self.size, self.fp.tell(), (self.mode, 0, 1))
  41. ]
  42. break
  43. else:
  44. # read key/value pair
  45. # FIXME: dangerous, may read whole file
  46. s = s + self.fp.readline()
  47. if len(s) == 1 or len(s) > 100:
  48. break
  49. if s[0] == ord(b"*"):
  50. continue # comment
  51. m = field.match(s)
  52. if not m:
  53. break
  54. k, v = m.group(1, 2)
  55. if k == "width":
  56. xsize = int(v)
  57. self._size = xsize, ysize
  58. elif k == "height":
  59. ysize = int(v)
  60. self._size = xsize, ysize
  61. elif k == "pixel" and v == "n8":
  62. self.mode = "L"
  63. #
  64. # --------------------------------------------------------------------
  65. Image.register_open(ImtImageFile.format, ImtImageFile)
  66. #
  67. # no extension registered (".im" is simply too common)