Skip to content

Commit 86b5865

Browse files
authored
Merge pull request #168 from semjon00/main
Minor changes
2 parents 8d9fa63 + 2838dc5 commit 86b5865

File tree

2 files changed

+43
-52
lines changed

2 files changed

+43
-52
lines changed

scripts/depthmap.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
sys.path.append('extensions/stable-diffusion-webui-depthmap-script/scripts')
3838

39-
from stereoimage_generation import apply_stereo_divergence, overlap_red_cyan
39+
from stereoimage_generation import create_stereoimages
4040

4141
# midas imports
4242
from dmidas.dpt_depth import DPTDepthModel
@@ -103,13 +103,13 @@ def main_ui_panel(is_depth_tab):
103103

104104
with gr.Group():
105105
with gr.Row():
106-
gen_stereo = gr.Checkbox(label="Generate stereoscopic image", value=False)
106+
gen_stereo = gr.Checkbox(label="Generate stereoscopic image(s)", value=False)
107107
with gr.Group(visible=False) as stereo_options_row_0:
108108
with gr.Row(variant="compact"):
109109
stereo_mode_lr = gr.Checkbox(label="left-right", value=True)
110-
stereo_mode_rl = gr.Checkbox(label="right-left", value=True)
111-
stereo_mode_tb = gr.Checkbox(label="top-bottom", value=True)
112-
stereo_mode_bt = gr.Checkbox(label="bottom-top", value=True)
110+
stereo_mode_rl = gr.Checkbox(label="right-left", value=False)
111+
stereo_mode_tb = gr.Checkbox(label="top-bottom", value=False)
112+
stereo_mode_bt = gr.Checkbox(label="bottom-top", value=False)
113113
stereo_mode_an = gr.Checkbox(label="red-cyan-anaglyph", value=True)
114114

115115

@@ -586,44 +586,27 @@ def run_depthmap(processed, outpath, inputimages, inputnames,
586586

587587
if gen_stereo:
588588
print("Generating stereoscopic images..")
589-
590-
original_image = np.asarray(inputimages[count])
591-
balance = (stereo_balance + 1) / 2
592-
left_eye = original_image if balance < 0.001 else \
593-
apply_stereo_divergence(original_image, img_output, -1 * stereo_divergence * (balance), stereo_fill)
594-
right_eye = original_image if balance > 0.999 else \
595-
apply_stereo_divergence(original_image, img_output, +1 * stereo_divergence * (1 - balance), stereo_fill)
596-
597-
stereoimages = []
598-
stereonames = []
599-
if stereo_mode_lr:
600-
stereonames.append("left-right")
601-
stereoimages.append(Image.fromarray(np.hstack([left_eye, right_eye])))
602-
if stereo_mode_rl:
603-
stereonames.append("right-left")
604-
stereoimages.append(Image.fromarray(np.hstack([right_eye, left_eye])))
605-
if stereo_mode_tb:
606-
stereonames.append("top-bottom")
607-
stereoimages.append(Image.fromarray(np.vstack([left_eye, right_eye])))
608-
if stereo_mode_bt:
609-
stereonames.append("bottom-top")
610-
stereoimages.append(Image.fromarray(np.vstack([right_eye, left_eye])))
611-
if stereo_mode_an:
612-
stereonames.append("red-cyan-anaglyph")
613-
stereoimages.append(Image.fromarray(overlap_red_cyan(left_eye, right_eye)))
589+
590+
stereomodes = []
591+
if stereo_mode_lr: stereomodes.append("left-right")
592+
if stereo_mode_rl: stereomodes.append("right-left")
593+
if stereo_mode_tb: stereomodes.append("top-bottom")
594+
if stereo_mode_bt: stereomodes.append("bottom-top")
595+
if stereo_mode_an: stereomodes.append("red-cyan-anaglyph")
596+
stereoimages = create_stereoimages(inputimages[count], img_output, stereo_divergence, stereomodes, stereo_balance, stereo_fill)
614597

615598
for c in range(0, len(stereoimages)):
616599
outimages.append(stereoimages[c])
617600
if processed is not None:
618601
images.save_image(stereoimages[c], outpath, "", processed.all_seeds[count],
619602
processed.all_prompts[count], opts.samples_format, info=info, p=processed,
620-
suffix=f"_{stereonames[c]}")
603+
suffix=f"_{stereomodes[c]}")
621604
else:
622605
# from tab
623606
images.save_image(stereoimages[c], path=outpath, basename=basename, seed=None,
624607
prompt=None, extension=opts.samples_format, info=info, short_filename=True,
625608
no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=None,
626-
forced_filename=None, suffix=f"_{stereonames[c]}")
609+
forced_filename=None, suffix=f"_{stereomodes[c]}")
627610

628611
if gen_normal:
629612
# taken from @graemeniedermayer, hidden, for api use only, will remove in future.

scripts/stereoimage_generation.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,51 @@
22
import numpy as np
33
from PIL import Image
44

5-
def create_stereoimage(original_image, depthmap, divergence, mode='left-right', stereo_balance=0.0,
6-
fill_technique='polylines_sharp'):
7-
"""Creates a stereoscopic image.
8-
An effort is made to make it look nice, but beware that the resulting image will have some distortion .
5+
def create_stereoimages(original_image, depthmap, divergence, modes=None, stereo_balance=0.0,
6+
fill_technique='polylines_sharp'):
7+
"""Creates stereoscopic images.
8+
An effort is made to make them look nice, but beware that the resulting image will have some distortion .
99
1010
:param original_image: original image from which the 3D image (stereoimage) will be created
1111
:param depthmap: depthmap corresponding to the original image
1212
:param float divergence: the measure of 3D effect, in percentages.
1313
A good value will likely be somewhere in the [0.05; 10.0) interval.
14-
:param str mode: how the result will look like. The default is 'left-right'
15-
--- a picture for the left eye will be on the left and the picture from the right eye --- on the rigth.
14+
:param list modes: how the result will look like. By default only 'left-right' is generated
15+
- a picture for the left eye will be on the left and the picture from the right eye - on the rigth.
1616
The supported modes are: 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'red-cyan-anaglyph'.
1717
:param float stereo_balance: has to do with how the divergence will be split among the two parts of the image,
1818
must be in the [-1.0; 1.0] interval.
1919
:param str fill_technique: applying divergence inevidably creates some gaps in the image.
2020
This parameter specifies the technique that will be used to fill in the blanks in the two resulting images.
2121
Must be one of the following: 'none', 'naive', 'naive_interpolating', 'polylines_soft', 'polylines_sharp'.
2222
"""
23+
if modes is None:
24+
modes = ['left-right']
25+
if not isinstance(modes, list):
26+
modes = [modes]
27+
2328
original_image = np.asarray(original_image)
2429
balance = (stereo_balance + 1) / 2
2530
left_eye = original_image if balance < 0.001 else \
26-
apply_stereo_divergence(original_image, depthmap, -1 * divergence * (balance), fill_technique)
31+
apply_stereo_divergence(original_image, depthmap, -1 * divergence * balance, fill_technique)
2732
right_eye = original_image if balance > 0.999 else \
2833
apply_stereo_divergence(original_image, depthmap, +1 * divergence * (1 - balance), fill_technique)
29-
if mode == 'left-right':
30-
result = np.hstack([left_eye, right_eye])
31-
elif mode == 'right-left':
32-
result = np.hstack([right_eye, left_eye])
33-
elif mode == 'top-bottom':
34-
result = np.vstack([left_eye, right_eye])
35-
elif mode == 'bottom-top':
36-
result = np.vstack([right_eye, left_eye])
37-
elif mode == 'red-cyan-anaglyph':
38-
result = overlap_red_cyan(left_eye, right_eye)
39-
else:
40-
raise Exception('Unknown mode')
41-
return Image.fromarray(result)
34+
35+
results = []
36+
for mode in modes:
37+
if mode == 'left-right':
38+
results.append(np.hstack([left_eye, right_eye]))
39+
elif mode == 'right-left':
40+
results.append(np.hstack([right_eye, left_eye]))
41+
elif mode == 'top-bottom':
42+
results.append(np.vstack([left_eye, right_eye]))
43+
elif mode == 'bottom-top':
44+
results.append(np.vstack([right_eye, left_eye]))
45+
elif mode == 'red-cyan-anaglyph':
46+
results.append(overlap_red_cyan(left_eye, right_eye))
47+
else:
48+
raise Exception('Unknown mode')
49+
return [Image.fromarray(r) for r in results]
4250

4351

4452
def apply_stereo_divergence(original_image, depth, divergence, fill_technique):

0 commit comments

Comments
 (0)