fffiloni commited on
Commit
f118b28
·
verified ·
1 Parent(s): aec5cde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -17
app.py CHANGED
@@ -10,8 +10,8 @@ matrices = {
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
 
@@ -33,8 +33,8 @@ def make_anaglyph(left_img, right_img):
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):
@@ -50,10 +50,55 @@ def make_anaglyph(left_img, right_img):
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():
@@ -61,23 +106,43 @@ with gr.Blocks(title="3D Anaglyph Generator") as app:
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
 
 
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, color_method):
14
+ """Generate an anaglyph from left and right images using the specified color method"""
15
  if left_img is None or right_img is None:
16
  return None
17
 
 
33
  rightMap = right.load()
34
  resultMap = result.load()
35
 
36
+ # Use the selected color matrix
37
+ m = matrices[color_method]
38
 
39
  # Apply the anaglyph transformation
40
  for y in range(0, height):
 
50
  # Convert back to numpy array for Gradio
51
  return np.array(result)
52
 
53
+ def make_stereopair(left_img, right_img, color_method):
54
+ """Generate a stereo pair from left and right images"""
55
+ if left_img is None or right_img is None:
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:
64
+ # Resize right image to match left image dimensions
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':
83
+ pair = pair.convert('L')
84
+
85
+ # Convert back to numpy array for Gradio
86
+ return np.array(pair)
87
+
88
+ def process_images(left_img, right_img, method, color_method):
89
+ """Process images based on the selected method"""
90
+ if method == "anaglyph":
91
+ return make_anaglyph(left_img, right_img, color_method)
92
+ elif method == "parallel":
93
+ return make_stereopair(left_img, right_img, color_method)
94
+ elif method == "crossed":
95
+ return make_stereopair(right_img, left_img, color_method)
96
+ return None
97
+
98
  # Create the Gradio interface
99
+ with gr.Blocks(title="3D Image Generator") as app:
100
+ gr.Markdown("# 3D Image Generator")
101
+ gr.Markdown("Upload left and right images to create 3D images using different methods.")
102
 
103
  with gr.Row():
104
  with gr.Column():
 
106
  with gr.Column():
107
  right_input = gr.Image(label="Right Image")
108
 
109
+ with gr.Row():
110
+ with gr.Column():
111
+ method = gr.Radio(
112
+ ["anaglyph", "parallel", "crossed"],
113
+ label="Method",
114
+ value="anaglyph",
115
+ info="Select the 3D image creation method"
116
+ )
117
+ with gr.Column():
118
+ color_method = gr.Radio(
119
+ ["optimized", "true", "mono", "color", "halfcolor"],
120
+ label="Color Method",
121
+ value="optimized",
122
+ info="Select the color processing method"
123
+ )
124
+
125
+ generate_btn = gr.Button("Generate 3D Image", variant="primary")
126
 
127
+ output = gr.Image(label="Generated 3D Image")
128
 
129
  gr.Markdown("""
130
+ ### Methods:
131
+ - **anaglyph**: Creates a red-cyan 3D image (requires 3D glasses)
132
+ - **parallel**: Creates side-by-side images for parallel viewing
133
+ - **crossed**: Creates side-by-side images for cross-eyed viewing
 
134
 
135
+ ### Color Methods:
136
+ - **optimized**: Best for most images (default)
137
+ - **true**: True color anaglyph
138
+ - **mono**: Monochrome output
139
+ - **color**: Full color (may cause ghosting)
140
+ - **halfcolor**: Balance between color and depth
141
  """)
142
 
143
  generate_btn.click(
144
+ fn=process_images,
145
+ inputs=[left_input, right_input, method, color_method],
146
  outputs=output
147
  )
148