ColorSpaceTransform.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. from . import Processing
  4. import colour, copy, os
  5. import miam.image.Image as MIMG
  6. import miam.image.ColorSpace as MICS
  7. import miam.image.imageType
  8. import miam.histogram.Histogram as MHIST
  9. import miam.aesthetics.LightnessAesthetics as MAE
  10. import matplotlib.pyplot as plt
  11. import numpy as np
  12. # could be remove after check
  13. # import skimage.color
  14. # ------------------------------------------------------------------------------------------
  15. # MIAM project 2020
  16. # ------------------------------------------------------------------------------------------
  17. # author: remi.cozot@univ-littoral.fr
  18. # ------------------------------------------------------------------------------------------
  19. class ColorSpaceTransform(object):
  20. """description of class"""
  21. def __init__(self):
  22. pass
  23. def compute(self, img, **kwargs):
  24. # first create a copy
  25. res = copy.deepcopy(img)
  26. if not kwargs:
  27. # " print WARNING message"
  28. print("WARNING[miam.processing.ColorSpaceTransform(",img.name,"):", "no destination colour space >> return a copy of image]")
  29. else:
  30. if not 'dest'in kwargs:
  31. print("WARNING[miam.processing.ColorSpaceTransform(",img.name,"):", "no 'dest' colour space >> return a copy of image]")
  32. else: # -> 'colorSpace' found
  33. if kwargs['dest'] == 'Lab':
  34. # ---------------------------------------------------------------------------------------------------------------
  35. # DEST: Lab
  36. # ----------------------------------------------------------------------------------------------------------------
  37. currentCS = img.colorSpace.name
  38. # sRGB to Lab
  39. if currentCS=="sRGB":
  40. # ---------------------------------------------------------------------------------------------------------------
  41. # sRGB to Lab
  42. # ---------------------------------------------------------------------------------------------------------------
  43. if not img.linear: apply_cctf_decoding=True
  44. else: apply_cctf_decoding=False
  45. # DEBUG
  46. #print("DEBUG[sRGB > Lab]:apply_cctf_decoding>>",apply_cctf_decoding)
  47. # sRGB to XYZ
  48. RGB = res.colorData
  49. XYZ = colour.sRGB_to_XYZ(RGB, illuminant=np.array([ 0.3127, 0.329 ]), chromatic_adaptation_method='CAT02', apply_cctf_decoding=apply_cctf_decoding)
  50. # XYZ to Lab
  51. Lab = colour.XYZ_to_Lab(XYZ, illuminant=np.array([ 0.3127, 0.329 ]))
  52. # update res
  53. res.colorData = Lab
  54. res.linear = None
  55. res.colorSpace = MICS.ColorSpace.buildLab()
  56. # XYZ to Lab
  57. elif currentCS=="XYZ": # XYZ -> Lab
  58. # ---------------------------------------------------------------------------------------------------------------
  59. # XYZ to Lab
  60. # ---------------------------------------------------------------------------------------------------------------
  61. XYZ = res.colorData
  62. # XYZ to Lab
  63. Lab = colour.XYZ_to_Lab(XYZ, illuminant=np.array([ 0.3127, 0.329 ]))
  64. # update res
  65. res.colorData = Lab
  66. res.linear = None
  67. res.colorSpace = MICS.ColorSpace.buildXYZ()
  68. # Lab to Lab
  69. elif currentCS == "Lab":
  70. # ---------------------------------------------------------------------------------------------------------------
  71. # Lab to Lab
  72. # ---------------------------------------------------------------------------------------------------------------
  73. # return a copy
  74. pass
  75. elif kwargs['dest'] == 'sRGB':
  76. # ---------------------------------------------------------------------------------------------------------------
  77. # DEST: sRGB
  78. # ----------------------------------------------------------------------------------------------------------------
  79. currentCS = img.colorSpace.name
  80. # Lab to sRGB
  81. if currentCS=="Lab":
  82. # ---------------------------------------------------------------------------------------------------------------
  83. # Lab to sRGB
  84. # ---------------------------------------------------------------------------------------------------------------
  85. Lab = res.colorData
  86. # Lab to XYZ
  87. XYZ = colour.Lab_to_XYZ(Lab, illuminant=np.array([ 0.3127, 0.329 ]))
  88. # XYZ to sRGB
  89. if img.type == MIMG.imageType.imageType.HDR : apply_cctf_encoding = False
  90. else : apply_cctf_encoding = True
  91. sRGB = colour.colour.XYZ_to_sRGB(XYZ, illuminant=np.array([ 0.3127, 0.329 ]), chromatic_adaptation_transform='CAT02', apply_cctf_encoding=apply_cctf_encoding)
  92. # update res
  93. res.colorData = sRGB
  94. res.colorSpace = MICS.ColorSpace.buildsRGB()
  95. res.linear = not apply_cctf_encoding
  96. # XYZ to sRGB
  97. elif currentCS == "XYZ":
  98. # ---------------------------------------------------------------------------------------------------------------
  99. # XYZ to sRGB
  100. # ---------------------------------------------------------------------------------------------------------------
  101. XYZ = res.colorData
  102. # XYZ to sRGB
  103. if img.type == MIMG.imageType.imageType.HDR : apply_cctf_encoding = False
  104. else: apply_cctf_encoding = True
  105. sRGB = colour.colour.XYZ_to_sRGB(XYZ, illuminant=np.array([ 0.3127, 0.329 ]), chromatic_adaptation_transform='CAT02', apply_cctf_encoding=apply_cctf_encoding)
  106. # update res
  107. res.colorData = sRGB
  108. res.colorSpace = MICS.ColorSpace.buildsRGB()
  109. res.linear = not apply_cctf_encoding
  110. # sRGB to sRGB
  111. elif currentCS == "sRGB":
  112. # return a copy
  113. pass
  114. else:
  115. print("WARNING[miam.processing.ColorSpaceTransform(",img.name,"):", "'dest' colour space:",kwargs['dest'] , "not yet implemented !]")
  116. elif kwargs['dest'] == 'XYZ':
  117. # ---------------------------------------------------------------------------------------------------------------
  118. # DEST: XYZ
  119. # ----------------------------------------------------------------------------------------------------------------
  120. currentCS = img.colorSpace.name
  121. # sRGB to XYZ
  122. if currentCS=="sRGB":
  123. # ---------------------------------------------------------------------------------------------------------------
  124. # sRGB to XYZ
  125. # ---------------------------------------------------------------------------------------------------------------
  126. if img.type == MIMG.imageType.imageType.SDR :
  127. apply_cctf_decoding=True
  128. else:
  129. apply_cctf_decoding=False
  130. # DEBUG
  131. #print("DEBUG[sRGB > XYZ]:apply_cctf_decoding>>",apply_cctf_decoding)
  132. # sRGB to XYZ
  133. RGB = res.colorData
  134. XYZ = colour.sRGB_to_XYZ(RGB, illuminant=np.array([ 0.3127, 0.329 ]), chromatic_adaptation_method='CAT02', apply_cctf_decoding=apply_cctf_decoding)
  135. # update res
  136. res.colorData = XYZ
  137. res.linear = True
  138. res.colorSpace = MICS.ColorSpace.buildXYZ()
  139. # Lab to XYZ
  140. elif currentCS=="XYZ":
  141. # ---------------------------------------------------------------------------------------------------------------
  142. # XYZ to XYZ
  143. # ---------------------------------------------------------------------------------------------------------------
  144. # return a copy
  145. pass
  146. # Lab to XYZ
  147. elif currentCS == "Lab":
  148. # ---------------------------------------------------------------------------------------------------------------
  149. # Lab to XYZ
  150. # ---------------------------------------------------------------------------------------------------------------
  151. Lab = res.colorData
  152. # Lab to XYZ
  153. XYZ = colour.Lab_to_XYZ(Lab, illuminant=np.array([ 0.3127, 0.329 ]))
  154. # update res
  155. res.colorData = XYZ
  156. res.linear = True
  157. res.colorSpace = MICS.ColorSpace.buildXYZ()
  158. return res
  159. def XYZ_to_sRGB(XYZ, apply_cctf_encoding=True):
  160. RGB = colour.XYZ_to_sRGB(XYZ, illuminant=np.array([ 0.3127, 0.329 ]), chromatic_adaptation_transform='CAT02', apply_cctf_encoding=apply_cctf_encoding)
  161. return RGB
  162. def sRGB_to_XYZ(RGB, apply_cctf_decoding=True):
  163. XYZ = colour.sRGB_to_XYZ(RGB, illuminant=np.array([ 0.3127, 0.329 ]), chromatic_adaptation_method='CAT02', apply_cctf_decoding=apply_cctf_decoding)
  164. return XYZ
  165. def Lab_to_XYZ(Lab, apply_cctf_encoding=True):
  166. XYZ = colour.Lab_to_XYZ(Lab, illuminant=np.array([ 0.3127, 0.329 ]))
  167. return XYZ
  168. def XYZ_to_Lab(XYZ, apply_cctf_decoding=True):
  169. Lab = colour.XYZ_to_Lab(XYZ, illuminant=np.array([ 0.3127, 0.329 ]))
  170. return Lab
  171. def Lab_to_sRGB(Lab, apply_cctf_encoding=True):
  172. XYZ = colour.Lab_to_XYZ(Lab, illuminant=np.array([ 0.3127, 0.329 ]))
  173. RGB = colour.XYZ_to_sRGB(XYZ, illuminant=np.array([ 0.3127, 0.329 ]), chromatic_adaptation_transform='CAT02', apply_cctf_encoding=apply_cctf_encoding)
  174. return RGB
  175. def sRGB_to_Lab(RGB, apply_cctf_decoding=True):
  176. XYZ = colour.sRGB_to_XYZ(RGB, illuminant=np.array([ 0.3127, 0.329 ]), chromatic_adaptation_method='CAT02', apply_cctf_decoding=apply_cctf_decoding)
  177. Lab = colour.XYZ_to_Lab(XYZ, illuminant=np.array([ 0.3127, 0.329 ]))
  178. return Lab