ImageMode.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # standard mode descriptors
  6. #
  7. # History:
  8. # 2006-03-20 fl Added
  9. #
  10. # Copyright (c) 2006 by Secret Labs AB.
  11. # Copyright (c) 2006 by Fredrik Lundh.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15. # mode descriptor cache
  16. _modes = None
  17. class ModeDescriptor:
  18. """Wrapper for mode strings."""
  19. def __init__(self, mode, bands, basemode, basetype):
  20. self.mode = mode
  21. self.bands = bands
  22. self.basemode = basemode
  23. self.basetype = basetype
  24. def __str__(self):
  25. return self.mode
  26. def getmode(mode):
  27. """Gets a mode descriptor for the given mode."""
  28. global _modes
  29. if not _modes:
  30. # initialize mode cache
  31. modes = {}
  32. for m, (basemode, basetype, bands) in {
  33. # core modes
  34. "1": ("L", "L", ("1",)),
  35. "L": ("L", "L", ("L",)),
  36. "I": ("L", "I", ("I",)),
  37. "F": ("L", "F", ("F",)),
  38. "P": ("P", "L", ("P",)),
  39. "RGB": ("RGB", "L", ("R", "G", "B")),
  40. "RGBX": ("RGB", "L", ("R", "G", "B", "X")),
  41. "RGBA": ("RGB", "L", ("R", "G", "B", "A")),
  42. "CMYK": ("RGB", "L", ("C", "M", "Y", "K")),
  43. "YCbCr": ("RGB", "L", ("Y", "Cb", "Cr")),
  44. "LAB": ("RGB", "L", ("L", "A", "B")),
  45. "HSV": ("RGB", "L", ("H", "S", "V")),
  46. # extra experimental modes
  47. "RGBa": ("RGB", "L", ("R", "G", "B", "a")),
  48. "LA": ("L", "L", ("L", "A")),
  49. "La": ("L", "L", ("L", "a")),
  50. "PA": ("RGB", "L", ("P", "A")),
  51. }.items():
  52. modes[m] = ModeDescriptor(m, bands, basemode, basetype)
  53. # mapping modes
  54. for i16mode in (
  55. "I;16",
  56. "I;16S",
  57. "I;16L",
  58. "I;16LS",
  59. "I;16B",
  60. "I;16BS",
  61. "I;16N",
  62. "I;16NS",
  63. ):
  64. modes[i16mode] = ModeDescriptor(i16mode, ("I",), "L", "L")
  65. # set global mode cache atomically
  66. _modes = modes
  67. return _modes[mode]