File size: 2,996 Bytes
aec5cde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import gradio as gr
from PIL import Image
import numpy as np

matrices = {
    '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 ] ],
    '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 ] ],
    'color': [ [ 1, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 0, 1 ] ],
    'halfcolor': [ [ 0.299, 0.587, 0.114, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 0, 1 ] ],
    'optimized': [ [ 0, 0.7, 0.3, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 1, 0, 0, 0, 1 ] ],
}

def make_anaglyph(left_img, right_img):
    """Generate an optimized anaglyph from left and right images"""
    if left_img is None or right_img is None:
        return None
    
    # Convert from numpy array (from Gradio) to PIL Image
    left = Image.fromarray(left_img)
    right = Image.fromarray(right_img)
    
    # Check if both images have the same dimensions
    if left.size != right.size:
        # Resize right image to match left image dimensions
        right = right.resize(left.size, Image.LANCZOS)
    
    # Create a copy of the left image to modify
    result = left.copy()
    
    # Get the pixel maps
    width, height = left.size
    leftMap = left.load()
    rightMap = right.load()
    resultMap = result.load()
    
    # Use the optimized color matrix
    m = matrices['optimized']
    
    # Apply the anaglyph transformation
    for y in range(0, height):
        for x in range(0, width):
            r1, g1, b1 = leftMap[x, y]
            r2, g2, b2 = rightMap[x, y]
            resultMap[x, y] = (
                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]),
                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]),
                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])
            )
    
    # Convert back to numpy array for Gradio
    return np.array(result)

# Create the Gradio interface
with gr.Blocks(title="3D Anaglyph Generator") as app:
    gr.Markdown("# 3D Anaglyph Generator")
    gr.Markdown("Upload left and right images to create a 3D anaglyph with optimized color settings.")
    
    with gr.Row():
        with gr.Column():
            left_input = gr.Image(label="Left Image")
        with gr.Column():
            right_input = gr.Image(label="Right Image")
    
    generate_btn = gr.Button("Generate Anaglyph", variant="primary")
    
    output = gr.Image(label="Generated Anaglyph (Red-Cyan)")
    
    gr.Markdown("""
    ### How to use:
    1. Upload a left-eye image
    2. Upload a right-eye image
    3. Click "Generate Anaglyph"
    4. View or download the resulting anaglyph
    
    For best results, use red-cyan 3D glasses to view the generated anaglyph.
    """)
    
    generate_btn.click(
        fn=make_anaglyph,
        inputs=[left_input, right_input],
        outputs=output
    )

# Launch the app
if __name__ == "__main__":
    app.launch()