Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 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 ] ],
|
| 10 |
+
'optimized': [ [ 0, 0.7, 0.3, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 0, 1 ] ],
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def make_anaglyph(left_img, right_img):
|
| 14 |
+
"""Generate an optimized anaglyph from left and right images"""
|
| 15 |
+
if left_img is None or right_img is None:
|
| 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()
|
| 35 |
+
|
| 36 |
+
# Use the optimized color matrix
|
| 37 |
+
m = matrices['optimized']
|
| 38 |
+
|
| 39 |
+
# Apply the anaglyph transformation
|
| 40 |
+
for y in range(0, height):
|
| 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)
|
| 52 |
+
|
| 53 |
+
# Create the Gradio interface
|
| 54 |
+
with gr.Blocks(title="3D Anaglyph Generator") as app:
|
| 55 |
+
gr.Markdown("# 3D Anaglyph Generator")
|
| 56 |
+
gr.Markdown("Upload left and right images to create a 3D anaglyph with optimized color settings.")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
left_input = gr.Image(label="Left Image")
|
| 61 |
+
with gr.Column():
|
| 62 |
+
right_input = gr.Image(label="Right Image")
|
| 63 |
+
|
| 64 |
+
generate_btn = gr.Button("Generate Anaglyph", variant="primary")
|
| 65 |
+
|
| 66 |
+
output = gr.Image(label="Generated Anaglyph (Red-Cyan)")
|
| 67 |
+
|
| 68 |
+
gr.Markdown("""
|
| 69 |
+
### How to use:
|
| 70 |
+
1. Upload a left-eye image
|
| 71 |
+
2. Upload a right-eye image
|
| 72 |
+
3. Click "Generate Anaglyph"
|
| 73 |
+
4. View or download the resulting anaglyph
|
| 74 |
+
|
| 75 |
+
For best results, use red-cyan 3D glasses to view the generated anaglyph.
|
| 76 |
+
""")
|
| 77 |
+
|
| 78 |
+
generate_btn.click(
|
| 79 |
+
fn=make_anaglyph,
|
| 80 |
+
inputs=[left_input, right_input],
|
| 81 |
+
outputs=output
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Launch the app
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
app.launch()
|