PaletteFile.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #
  2. # Python Imaging Library
  3. # $Id$
  4. #
  5. # stuff to read simple, teragon-style palette files
  6. #
  7. # History:
  8. # 97-08-23 fl Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1997.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15. from ._binary import o8
  16. ##
  17. # File handler for Teragon-style palette files.
  18. class PaletteFile(object):
  19. rawmode = "RGB"
  20. def __init__(self, fp):
  21. self.palette = [(i, i, i) for i in range(256)]
  22. while True:
  23. s = fp.readline()
  24. if not s:
  25. break
  26. if s[0:1] == b"#":
  27. continue
  28. if len(s) > 100:
  29. raise SyntaxError("bad palette file")
  30. v = [int(x) for x in s.split()]
  31. try:
  32. [i, r, g, b] = v
  33. except ValueError:
  34. [i, r] = v
  35. g = b = r
  36. if 0 <= i <= 255:
  37. self.palette[i] = o8(r) + o8(g) + o8(b)
  38. self.palette = b"".join(self.palette)
  39. def getpalette(self):
  40. return self.palette, self.rawmode