Exposure.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import cv2
  2. import Tool
  3. from scipy import ndimage
  4. class Exposure(Tool.Tool):
  5. def on_init(self):
  6. self.id = "exposure"
  7. self.name = "Exposure"
  8. self.icon_path = "ui/PF2_Icons/Exposure.png"
  9. self.properties = [
  10. # Detailer
  11. Tool.Property("enabled", "Exposure", "Header", False, has_toggle=False, has_button=False),
  12. Tool.Property("exposure", "Overall Exposure", "Slider", 0, max=50, min=-50),
  13. Tool.Property("h_exposure", "Highlight Exposure", "Slider", 0, max=50, min=-50),
  14. Tool.Property("m_exposure", "Midtone Exposure", "Slider", 0, max=50, min=-50),
  15. Tool.Property("s_exposure", "Shadow Exposure", "Slider", 0, max=50, min=-50),
  16. ]
  17. def on_update(self, image):
  18. im = image
  19. if(not self.is_default()):
  20. ex = self.props["exposure"].get_value() * 0.0001
  21. hex = self.props["h_exposure"].get_value() * 0.0001
  22. mex = self.props["m_exposure"].get_value() * 0.0001
  23. sex = self.props["s_exposure"].get_value() * 0.0001
  24. if(hex != 0):
  25. ish = self._is_highlight(im)
  26. mul = 2**hex
  27. im[ish > 0] = im[ish > 0]*mul
  28. if(mex != 0):
  29. ish = self._is_midtone(im)
  30. mul = 2**mex
  31. im[ish > 0] = im[ish > 0] * mul
  32. if(sex != 0):
  33. ish = self._is_shadow(im)
  34. mul = 2**sex
  35. im[ish > 0] = im[ish > 0] * mul
  36. if(ex != 0):
  37. mul = 2 ** ex
  38. im = im * mul
  39. return im
  40. def _is_highlight(self, image, bleed_value = 6.0):
  41. bleed = float(image.max() / bleed_value)
  42. mif = image.max() / 3.0 * 2.0
  43. icopy = image.copy()
  44. icopy[icopy < mif - bleed] = 0.0
  45. icopy[(icopy < mif) * (icopy != 0.0)] = ((mif - (icopy[(icopy < mif) * (icopy != 0.0)])) / bleed) * -1 + 1
  46. icopy[icopy >= mif] = 1.0
  47. return icopy
  48. def _is_midtone(self, image, bleed_value = 6.0):
  49. bleed = float(image.max() / bleed_value)
  50. mif = image.max() / 3.0
  51. mir = image.max() / 3.0 * 2.0
  52. icopy = image.copy()
  53. icopy[icopy < mif - bleed] = 0.0
  54. icopy[icopy > mir + bleed] = 0.0
  55. icopy[(icopy < mif) * (icopy != 0.0)] = ((mif - (icopy[(icopy < mif) * (icopy != 0.0)])) / bleed) * -1 + 1
  56. icopy[(icopy > mir) * (icopy != 0.0)] = (((icopy[(icopy > mir) * (icopy != 0.0)]) - mir) / bleed) * -1 + 1
  57. icopy[(icopy >= mif) * (icopy <= mir)] = 1.0
  58. return icopy
  59. def _is_shadow(self, image, bleed_value=6.0):
  60. bleed = float(image.max() / bleed_value)
  61. mir = image.max() / 3.0
  62. icopy = image.copy()
  63. icopy[icopy <= mir] = 1.0
  64. icopy[icopy > mir + bleed] = 0.0
  65. icopy[icopy > mir] = (((icopy[(icopy > mir) * (icopy != 0.0)]) - mir) / bleed) * -1 + 1
  66. return icopy