PaletteFile.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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[0:1] == b"#":
  26. continue
  27. if len(s) > 100:
  28. raise SyntaxError("bad palette file")
  29. v = [int(x) for x in s.split()]
  30. try:
  31. [i, r, g, b] = v
  32. except ValueError:
  33. [i, r] = v
  34. g = b = r
  35. if 0 <= i <= 255:
  36. self.palette[i] = o8(r) + o8(g) + o8(b)
  37. self.palette = b"".join(self.palette)
  38. def getpalette(self):
  39. return self.palette, self.rawmode