Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,8 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
5 |
-
# Fixed matrices for proper red/cyan anaglyph viewing
|
6 |
matrices = {
|
7 |
-
|
8 |
-
'true': [ [ 1, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 0, 1 ] ],
|
9 |
'mono': [ [ 0.299, 0.587, 0.114, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114 ] ],
|
10 |
'color': [ [ 1, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 0, 1 ] ],
|
11 |
'halfcolor': [ [ 0.299, 0.587, 0.114, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 0, 1 ] ],
|
@@ -18,19 +16,19 @@ def make_anaglyph(left_img, right_img, color_method):
|
|
18 |
return None
|
19 |
|
20 |
# Convert from numpy array (from Gradio) to PIL Image
|
21 |
-
left = Image.fromarray(left_img)
|
22 |
-
right = Image.fromarray(right_img)
|
23 |
|
24 |
# Check if both images have the same dimensions
|
25 |
if left.size != right.size:
|
26 |
# Resize right image to match left image dimensions
|
27 |
right = right.resize(left.size, Image.LANCZOS)
|
28 |
|
29 |
-
# Create a
|
30 |
-
|
31 |
-
result = Image.new('RGB', (width, height))
|
32 |
|
33 |
# Get the pixel maps
|
|
|
34 |
leftMap = left.load()
|
35 |
rightMap = right.load()
|
36 |
resultMap = result.load()
|
@@ -43,18 +41,11 @@ def make_anaglyph(left_img, right_img, color_method):
|
|
43 |
for x in range(0, width):
|
44 |
r1, g1, b1 = leftMap[x, y]
|
45 |
r2, g2, b2 = rightMap[x, y]
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
# Ensure values are in valid range
|
53 |
-
r = max(0, min(255, r))
|
54 |
-
g = max(0, min(255, g))
|
55 |
-
b = max(0, min(255, b))
|
56 |
-
|
57 |
-
resultMap[x, y] = (r, g, b)
|
58 |
|
59 |
# Convert back to numpy array for Gradio
|
60 |
return np.array(result)
|
@@ -65,8 +56,8 @@ def make_stereopair(left_img, right_img, color_method):
|
|
65 |
return None
|
66 |
|
67 |
# Convert from numpy array (from Gradio) to PIL Image
|
68 |
-
left = Image.fromarray(left_img)
|
69 |
-
right = Image.fromarray(right_img)
|
70 |
|
71 |
# Check if both images have the same dimensions
|
72 |
if left.size != right.size:
|
@@ -74,13 +65,18 @@ def make_stereopair(left_img, right_img, color_method):
|
|
74 |
right = right.resize(left.size, Image.LANCZOS)
|
75 |
|
76 |
width, height = left.size
|
|
|
|
|
77 |
|
78 |
# Create a new image twice as wide
|
79 |
pair = Image.new('RGB', (width * 2, height))
|
|
|
80 |
|
81 |
-
#
|
82 |
-
|
83 |
-
|
|
|
|
|
84 |
|
85 |
# Convert to monochrome if required
|
86 |
if color_method == 'mono':
|
@@ -128,9 +124,9 @@ with gr.Blocks(css=css) as app:
|
|
128 |
)
|
129 |
|
130 |
color_method = gr.Radio(
|
131 |
-
["
|
132 |
label="Color Method",
|
133 |
-
value="
|
134 |
info="Select the color processing method"
|
135 |
)
|
136 |
|
@@ -143,11 +139,11 @@ with gr.Blocks(css=css) as app:
|
|
143 |
- **crossed**: Creates side-by-side images for cross-eyed viewing
|
144 |
|
145 |
### Color Methods:
|
146 |
-
- **
|
147 |
-
- **
|
148 |
-
- **optimized**: Better color perception but may cause ghosting
|
149 |
-
- **halfcolor**: Balance between color and depth
|
150 |
- **mono**: Monochrome output
|
|
|
|
|
151 |
""")
|
152 |
|
153 |
output = gr.Image(label="Generated 3D Anaglyph Image")
|
|
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
|
|
|
5 |
matrices = {
|
6 |
+
'true': [ [ 0.299, 0.587, 0.114, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0, 0, 0.299, 0.587, 0.114 ] ],
|
|
|
7 |
'mono': [ [ 0.299, 0.587, 0.114, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114 ] ],
|
8 |
'color': [ [ 1, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 0, 1 ] ],
|
9 |
'halfcolor': [ [ 0.299, 0.587, 0.114, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 0, 1 ] ],
|
|
|
16 |
return None
|
17 |
|
18 |
# Convert from numpy array (from Gradio) to PIL Image
|
19 |
+
left = Image.fromarray(left_img)
|
20 |
+
right = Image.fromarray(right_img)
|
21 |
|
22 |
# Check if both images have the same dimensions
|
23 |
if left.size != right.size:
|
24 |
# Resize right image to match left image dimensions
|
25 |
right = right.resize(left.size, Image.LANCZOS)
|
26 |
|
27 |
+
# Create a copy of the left image to modify
|
28 |
+
result = left.copy()
|
|
|
29 |
|
30 |
# Get the pixel maps
|
31 |
+
width, height = left.size
|
32 |
leftMap = left.load()
|
33 |
rightMap = right.load()
|
34 |
resultMap = result.load()
|
|
|
41 |
for x in range(0, width):
|
42 |
r1, g1, b1 = leftMap[x, y]
|
43 |
r2, g2, b2 = rightMap[x, y]
|
44 |
+
resultMap[x, y] = (
|
45 |
+
int(r1*m[0][0] + g1*m[0][1] + b1*m[0][2] + r2*m[1][0] + g2*m[1][1] + b2*m[1][2]),
|
46 |
+
int(r1*m[0][3] + g1*m[0][4] + b1*m[0][5] + r2*m[1][3] + g2*m[1][4] + b2*m[1][5]),
|
47 |
+
int(r1*m[0][6] + g1*m[0][7] + b1*m[0][8] + r2*m[1][6] + g2*m[1][7] + b2*m[1][8])
|
48 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# Convert back to numpy array for Gradio
|
51 |
return np.array(result)
|
|
|
56 |
return None
|
57 |
|
58 |
# Convert from numpy array (from Gradio) to PIL Image
|
59 |
+
left = Image.fromarray(left_img)
|
60 |
+
right = Image.fromarray(right_img)
|
61 |
|
62 |
# Check if both images have the same dimensions
|
63 |
if left.size != right.size:
|
|
|
65 |
right = right.resize(left.size, Image.LANCZOS)
|
66 |
|
67 |
width, height = left.size
|
68 |
+
leftMap = left.load()
|
69 |
+
rightMap = right.load()
|
70 |
|
71 |
# Create a new image twice as wide
|
72 |
pair = Image.new('RGB', (width * 2, height))
|
73 |
+
pairMap = pair.load()
|
74 |
|
75 |
+
# Copy the left and right images side by side
|
76 |
+
for y in range(0, height):
|
77 |
+
for x in range(0, width):
|
78 |
+
pairMap[x, y] = leftMap[x, y]
|
79 |
+
pairMap[x + width, y] = rightMap[x, y]
|
80 |
|
81 |
# Convert to monochrome if required
|
82 |
if color_method == 'mono':
|
|
|
124 |
)
|
125 |
|
126 |
color_method = gr.Radio(
|
127 |
+
["optimized", "true", "mono", "color", "halfcolor"],
|
128 |
label="Color Method",
|
129 |
+
value="optimized",
|
130 |
info="Select the color processing method"
|
131 |
)
|
132 |
|
|
|
139 |
- **crossed**: Creates side-by-side images for cross-eyed viewing
|
140 |
|
141 |
### Color Methods:
|
142 |
+
- **optimized**: Best for most images (default)
|
143 |
+
- **true**: True color anaglyph
|
|
|
|
|
144 |
- **mono**: Monochrome output
|
145 |
+
- **color**: Full color (may cause ghosting)
|
146 |
+
- **halfcolor**: Balance between color and depth
|
147 |
""")
|
148 |
|
149 |
output = gr.Image(label="Generated 3D Anaglyph Image")
|