WhiteBalance.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import cv2
  2. import numpy
  3. import Tool
  4. class WhiteBalance(Tool.Tool):
  5. def on_init(self):
  6. self.id = "whitebalance"
  7. self.name = "Colour Temperature"
  8. self.icon_path = "ui/PF2_Icons/WhiteBalance.png"
  9. self.properties = [
  10. Tool.Property("enabled", "Colour Temperature", "Header", False, has_toggle=True, has_button=False),
  11. Tool.Property("kelvin", "Kelvins", "Slider", 6500, max=15000, min=1000),
  12. Tool.Property("strength", "Strength", "Slider", 25, max=100, min=0),
  13. ]
  14. def on_update(self, image):
  15. if(self.props["enabled"].get_value()):
  16. ct = self.props["kelvin"].get_value()/100.0
  17. st = self.props["strength"].get_value()
  18. bpp = float(str(image.dtype).replace("uint", "").replace("float", ""))
  19. np = float(2 ** bpp - 1)
  20. r = 0
  21. print(ct)
  22. if(ct <= 66):
  23. print(ct <= 66)
  24. r = 255
  25. else:
  26. r = ct - 60
  27. r = 329.698727446 * numpy.math.pow(r, -0.1332047592)
  28. if(r < 0):
  29. r = 0
  30. if(r > 255):
  31. r = 255
  32. g = 0
  33. if(ct <= 66):
  34. g = ct
  35. g = 99.4708025861 * numpy.math.log(g) - 161.1195681661
  36. if (g < 0):
  37. g = 0
  38. if (g > 255):
  39. g = 255
  40. else:
  41. g = ct - 60
  42. g = 288.1221695283 * numpy.math.pow(g, -0.0755148492)
  43. if (g < 0):
  44. g = 0
  45. if (g > 255):
  46. g = 255
  47. b = 0
  48. if(ct >= 66):
  49. b = 255
  50. elif(ct <= 19):
  51. b = 0
  52. else:
  53. b = ct - 10
  54. b = 138.5177312231 * numpy.math.log(b) - 305.0447927307
  55. if (b < 0):
  56. b = 0
  57. if (b > 255):
  58. b = 255
  59. r = (r/255.0)
  60. g = (g / 255.0)
  61. b = (b / 255.0)
  62. print(r, g, b)
  63. # Red
  64. # image[0:, 0:, 2] = (image[0:, 0:, 2] * (((st / 100.0)*-1)+1)) + ((st / 100.0) * (r - np/2.0))
  65. image[0:, 0:, 2] = image[0:, 0:, 2] * r
  66. # # Green
  67. # image[0:, 0:, 1] = (image[0:, 0:, 1] * (((st / 100.0)*-1)+1)) + ((st / 100.0) * (g - np/2.0))
  68. image[0:, 0:, 1] = image[0:, 0:, 1] * g
  69. # # Blue
  70. # image[0:, 0:, 0] = (image[0:, 0:, 0] * (((st / 100.0)*-1)+1)) + ((st / 100.0) * (b - np/2.0))
  71. image[0:, 0:, 0] = image[0:, 0:, 0] * b
  72. image[image < 0.0] = 0.0
  73. image[image > np] = np
  74. return image