Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,10 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
|
|
|
|
| 5 |
matrices = {
|
| 6 |
-
|
|
|
|
| 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,19 +18,19 @@ def make_anaglyph(left_img, right_img, color_method):
|
|
| 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
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
# Get the pixel maps
|
| 31 |
-
width, height = left.size
|
| 32 |
leftMap = left.load()
|
| 33 |
rightMap = right.load()
|
| 34 |
resultMap = result.load()
|
|
@@ -41,11 +43,18 @@ def make_anaglyph(left_img, right_img, color_method):
|
|
| 41 |
for x in range(0, width):
|
| 42 |
r1, g1, b1 = leftMap[x, y]
|
| 43 |
r2, g2, b2 = rightMap[x, y]
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Convert back to numpy array for Gradio
|
| 51 |
return np.array(result)
|
|
@@ -56,8 +65,8 @@ def make_stereopair(left_img, right_img, color_method):
|
|
| 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,18 +74,13 @@ def make_stereopair(left_img, right_img, color_method):
|
|
| 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 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 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,9 +128,9 @@ with gr.Blocks(css=css) as app:
|
|
| 124 |
)
|
| 125 |
|
| 126 |
color_method = gr.Radio(
|
| 127 |
-
["
|
| 128 |
label="Color Method",
|
| 129 |
-
value="
|
| 130 |
info="Select the color processing method"
|
| 131 |
)
|
| 132 |
|
|
@@ -139,11 +143,11 @@ with gr.Blocks(css=css) as app:
|
|
| 139 |
- **crossed**: Creates side-by-side images for cross-eyed viewing
|
| 140 |
|
| 141 |
### Color Methods:
|
| 142 |
-
- **
|
| 143 |
-
- **
|
| 144 |
-
- **
|
| 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")
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# Fixed matrices for proper red/cyan anaglyph viewing
|
| 6 |
matrices = {
|
| 7 |
+
# For true red/cyan anaglyphs, we need strict channel separation
|
| 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 |
return None
|
| 19 |
|
| 20 |
# Convert from numpy array (from Gradio) to PIL Image
|
| 21 |
+
left = Image.fromarray(left_img).convert('RGB')
|
| 22 |
+
right = Image.fromarray(right_img).convert('RGB')
|
| 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 new blank image to put the anaglyph into
|
| 30 |
+
width, height = left.size
|
| 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 |
for x in range(0, width):
|
| 44 |
r1, g1, b1 = leftMap[x, y]
|
| 45 |
r2, g2, b2 = rightMap[x, y]
|
| 46 |
+
|
| 47 |
+
# Calculate new RGB values
|
| 48 |
+
r = 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])
|
| 49 |
+
g = 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])
|
| 50 |
+
b = 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])
|
| 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 |
return None
|
| 66 |
|
| 67 |
# Convert from numpy array (from Gradio) to PIL Image
|
| 68 |
+
left = Image.fromarray(left_img).convert('RGB')
|
| 69 |
+
right = Image.fromarray(right_img).convert('RGB')
|
| 70 |
|
| 71 |
# Check if both images have the same dimensions
|
| 72 |
if left.size != right.size:
|
|
|
|
| 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 |
+
# Paste the left and right images side by side
|
| 82 |
+
pair.paste(left, (0, 0))
|
| 83 |
+
pair.paste(right, (width, 0))
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# Convert to monochrome if required
|
| 86 |
if color_method == 'mono':
|
|
|
|
| 128 |
)
|
| 129 |
|
| 130 |
color_method = gr.Radio(
|
| 131 |
+
["true", "color", "optimized", "halfcolor", "mono"],
|
| 132 |
label="Color Method",
|
| 133 |
+
value="true",
|
| 134 |
info="Select the color processing method"
|
| 135 |
)
|
| 136 |
|
|
|
|
| 143 |
- **crossed**: Creates side-by-side images for cross-eyed viewing
|
| 144 |
|
| 145 |
### Color Methods:
|
| 146 |
+
- **true**: Pure red-cyan anaglyph (best for 3D glasses)
|
| 147 |
+
- **color**: Same as true for red-cyan glasses
|
| 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")
|