PaletteFile.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. class PaletteFile:
  17. """File handler for Teragon-style palette files."""
  18. rawmode = "RGB"
  19. def __init__(self, fp):
  20. self.palette = [(i, i, i) for i in range(256)]
  21. while True:
  22. s = fp.readline()
  23. if not s:
  24. break
  25. if s[:1] == b"#":
  26. continue
  27. if len(s) > 100:
  28. msg = "bad palette file"
  29. raise SyntaxError(msg)
  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