home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pypil112.zip / PIL-1.1.2.zip / Lib / site-packages / PIL / ImtImagePlugin.py < prev    next >
Text File  |  2001-05-03  |  2KB  |  91 lines

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