ColorSpace.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. import os, colour
  4. # local
  5. from . import Exif
  6. # ------------------------------------------------------------------------------------------
  7. # MIAM project 2020
  8. # ------------------------------------------------------------------------------------------
  9. # author: remi.cozot@univ-littoral.fr
  10. # ------------------------------------------------------------------------------------------
  11. class ColorSpace(object):
  12. """mapping to colour.models.RGB_COLOURSPACES"""
  13. def buildFromExifData(dictExif, defaultCS = 'sRGB'):
  14. """ build colour.models from exif dict (dict) """
  15. # default color space
  16. colourspace= None
  17. exifColorSpace = ['Color Space', 'Profile Description']
  18. # first 'Color Space'
  19. if exifColorSpace[0] in dictExif:
  20. value = dictExif[exifColorSpace[0]]
  21. if 'sRGB' in value :
  22. colourspace= colour.models.RGB_COLOURSPACES['sRGB'].copy()
  23. elif 'Adobe' in value :
  24. colourspace= colour.models.RGB_COLOURSPACES['Adobe RGB (1998)'].copy()
  25. # no color space found, try profile description
  26. if not colourspace:
  27. if exifColorSpace[1] in dictExif:
  28. value = dictExif[exifColorSpace[1]]
  29. if 'sRGB' in value :
  30. colourspace= colour.models.RGB_COLOURSPACES['sRGB'].copy()
  31. elif 'Adobe' in value :
  32. colourspace= colour.models.RGB_COLOURSPACES['Adobe RGB (1998)'].copy()
  33. # no color space found, use default
  34. if not colourspace:
  35. # warning
  36. # print("WARNING[miam.image.ColorSpace.buildFromExif(",dictExif['File Name'],"):", "no colour space found in Exif Metadata >> ", defaultCS, " used by default]")
  37. colourspace = colour.models.RGB_COLOURSPACES[defaultCS].copy()
  38. return colourspace
  39. def buildFromExif(e, defaultCS = 'sRGB'):
  40. """ build colour.models from Exif (object) """
  41. return ColorSpace.buildFromExifData(e.exif, defaultCS)
  42. def buildLab():
  43. return colour.RGB_Colourspace('Lab', primaries = None, whitepoint = None, whitepoint_name=None,
  44. RGB_to_XYZ_matrix=None, XYZ_to_RGB_matrix=None,
  45. cctf_encoding=None, cctf_decoding=None,
  46. use_derived_RGB_to_XYZ_matrix=False, use_derived_XYZ_to_RGB_matrix=False)
  47. def buildsRGB(): return colour.models.RGB_COLOURSPACES['sRGB'].copy()
  48. def buildXYZ():
  49. return colour.RGB_Colourspace('XYZ', primaries = None, whitepoint = None, whitepoint_name=None,
  50. RGB_to_XYZ_matrix=None, XYZ_to_RGB_matrix=None,
  51. cctf_encoding=None, cctf_decoding=None,
  52. use_derived_RGB_to_XYZ_matrix=False, use_derived_XYZ_to_RGB_matrix=False)
  53. def build(name='sRGB'):
  54. cs = None
  55. if name== 'sRGB': cs = colour.models.RGB_COLOURSPACES['sRGB'].copy()
  56. if name== 'Lab' : cs = buildLab()
  57. if name== 'XYZ' : cs = buildXYZ()
  58. return cs