ImtImagePlugin.py 2.3 KB

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