Spaces:
Running
Running
Create modules/lut.py
#2
by Jacky2305 - opened
- modules/lut.py +47 -0
modules/lut.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
LUTS = {
|
| 4 |
+
"cinematic": {
|
| 5 |
+
"name": "π¬ η΅ε½±ζ©ι",
|
| 6 |
+
"matrix": np.array([[1.1, 0.0, -0.1], [0.0, 1.05, 0.0], [-0.05, 0.0, 1.15]]),
|
| 7 |
+
"warmth": (5, 0, -5), "contrast": 1.15,
|
| 8 |
+
},
|
| 9 |
+
"bw": {
|
| 10 |
+
"name": "β¬ η»ε
Έι»η½",
|
| 11 |
+
"matrix": np.array([[0.3, 0.59, 0.11], [0.3, 0.59, 0.11], [0.3, 0.59, 0.11]]),
|
| 12 |
+
"warmth": (0, 0, 0), "contrast": 1.3,
|
| 13 |
+
},
|
| 14 |
+
"cyberpunk": {
|
| 15 |
+
"name": "π θ΅εζε
",
|
| 16 |
+
"matrix": np.array([[0.8, 0.0, 0.3], [0.0, 0.9, 0.1], [0.1, 0.0, 1.3]]),
|
| 17 |
+
"warmth": (-10, 0, 15), "contrast": 1.2,
|
| 18 |
+
},
|
| 19 |
+
"warm": {
|
| 20 |
+
"name": "βοΈ ζ₯η³»ζι³",
|
| 21 |
+
"matrix": np.array([[1.1, 0.05, 0.0], [0.0, 1.0, -0.05], [-0.05, -0.05, 0.9]]),
|
| 22 |
+
"warmth": (15, 5, -10), "contrast": 0.95,
|
| 23 |
+
},
|
| 24 |
+
"noir": {
|
| 25 |
+
"name": "π ε€ζ―θθ°",
|
| 26 |
+
"matrix": np.array([[0.7, 0.0, 0.2], [0.0, 0.8, 0.1], [0.1, 0.0, 1.2]]),
|
| 27 |
+
"warmth": (-15, -5, 10), "contrast": 1.25,
|
| 28 |
+
},
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def apply_lut(frame, lut_name):
|
| 32 |
+
lut = LUTS.get(lut_name)
|
| 33 |
+
if not lut:
|
| 34 |
+
return frame
|
| 35 |
+
frame_float = frame.astype(np.float32)
|
| 36 |
+
h, w = frame.shape[:2]
|
| 37 |
+
pixels = frame_float.reshape(-1, 3)
|
| 38 |
+
transformed = pixels @ lut["matrix"].T
|
| 39 |
+
transformed = np.clip(transformed, 0, 255).reshape(h, w, 3)
|
| 40 |
+
b, g, r = cv2.split(transformed.astype(np.uint8))
|
| 41 |
+
r = cv2.add(r, lut["warmth"][0])
|
| 42 |
+
g = cv2.add(g, lut["warmth"][1])
|
| 43 |
+
b = cv2.add(b, lut["warmth"][2])
|
| 44 |
+
frame = cv2.merge([b, g, r])
|
| 45 |
+
frame = frame.astype(np.float32)
|
| 46 |
+
frame = (frame - 128) * lut["contrast"] + 128
|
| 47 |
+
return np.clip(frame, 0, 255).astype(np.uint8)
|