BlackWhite.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import cv2
  2. import numpy
  3. import Tool
  4. class BlackWhite(Tool.Tool):
  5. def on_init(self):
  6. self.id = "black_white"
  7. self.name = "Black and White"
  8. self.icon_path = "ui/PF2_Icons/BlackWhite.png"
  9. self.properties = [
  10. Tool.Property("enabled", "Black and White", "Header", False, has_toggle=True, has_button=False),
  11. Tool.Property("method", "Method", "Combo", 0, options=[
  12. "Average",
  13. "Weighted Average",
  14. "Luma",
  15. "Custom Weight"
  16. ]),
  17. Tool.Property("customHeader", "Custom Weight", "Header", None, has_toggle=False, has_button=False, is_subheading=True),
  18. Tool.Property("red", "Red Value", "Spin", 0.333, max=1, min=0),
  19. Tool.Property("green", "Green Value", "Spin", 0.333, max=1, min=0),
  20. Tool.Property("blue", "Blue Value", "Spin", 0.333, max=1, min=0),
  21. ]
  22. def on_update(self, image):
  23. if(self.props["enabled"].get_value()):
  24. mode = self.props["method"].get_value()
  25. bpp = float(str(image.dtype).replace("uint", "").replace("float", ""))
  26. np = float(2 ** bpp - 1)
  27. out = image.astype(numpy.float32)
  28. if(mode == 0):
  29. bc = out[0:, 0:, 0]
  30. gc = out[0:, 0:, 1]
  31. rc = out[0:, 0:, 2]
  32. out = (bc + gc + rc) / 3
  33. elif(mode == 1):
  34. bc = out[0:, 0:, 0]
  35. gc = out[0:, 0:, 1]
  36. rc = out[0:, 0:, 2]
  37. out = 0.114 * bc + 0.587 * gc + 0.299 * rc
  38. elif(mode == 2):
  39. hsl = cv2.cvtColor(out, cv2.COLOR_BGR2HSV)
  40. out = hsl[0:, 0:, 2]
  41. elif(mode == 3):
  42. r = self.props["red"].get_value()
  43. g = self.props["green"].get_value()
  44. b = self.props["blue"].get_value()
  45. bc = out[0:, 0:, 0]
  46. gc = out[0:, 0:, 1]
  47. rc = out[0:, 0:, 2]
  48. out = b * bc + g * gc + r * rc
  49. out[out < 0.0] = 0.0
  50. out[out > np] = np
  51. out = cv2.cvtColor(out, cv2.COLOR_GRAY2BGR)
  52. return out.astype(image.dtype)
  53. else:
  54. return image