Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GimpPaletteFile.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #
  2. # Python Imaging Library
  3. # $Id$
  4. #
  5. # stuff to read GIMP palette files
  6. #
  7. # History:
  8. # 1997-08-23 fl Created
  9. # 2004-09-07 fl Support GIMP 2.0 palette files.
  10. #
  11. # Copyright (c) Secret Labs AB 1997-2004. All rights reserved.
  12. # Copyright (c) Fredrik Lundh 1997-2004.
  13. #
  14. # See the README file for information on usage and redistribution.
  15. #
  16. import re
  17. from ._binary import o8
  18. ##
  19. # File handler for GIMP's palette format.
  20. class GimpPaletteFile(object):
  21. rawmode = "RGB"
  22. def __init__(self, fp):
  23. self.palette = [o8(i)*3 for i in range(256)]
  24. if fp.readline()[:12] != b"GIMP Palette":
  25. raise SyntaxError("not a GIMP palette file")
  26. i = 0
  27. while i <= 255:
  28. s = fp.readline()
  29. if not s:
  30. break
  31. # skip fields and comment lines
  32. if re.match(br"\w+:|#", s):
  33. continue
  34. if len(s) > 100:
  35. raise SyntaxError("bad palette file")
  36. v = tuple(map(int, s.split()[:3]))
  37. if len(v) != 3:
  38. raise ValueError("bad palette entry")
  39. if 0 <= i <= 255:
  40. self.palette[i] = o8(v[0]) + o8(v[1]) + o8(v[2])
  41. i += 1
  42. self.palette = b"".join(self.palette)
  43. def getpalette(self):
  44. return self.palette, self.rawmode