Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 15 additions & 32 deletions scripts/depthmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

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

from stereoimage_generation import apply_stereo_divergence, overlap_red_cyan
from stereoimage_generation import create_stereoimages

# midas imports
from dmidas.dpt_depth import DPTDepthModel
Expand Down Expand Up @@ -103,13 +103,13 @@ def main_ui_panel(is_depth_tab):

with gr.Group():
with gr.Row():
gen_stereo = gr.Checkbox(label="Generate stereoscopic image", value=False)
gen_stereo = gr.Checkbox(label="Generate stereoscopic image(s)", value=False)
with gr.Group(visible=False) as stereo_options_row_0:
with gr.Row(variant="compact"):
stereo_mode_lr = gr.Checkbox(label="left-right", value=True)
stereo_mode_rl = gr.Checkbox(label="right-left", value=True)
stereo_mode_tb = gr.Checkbox(label="top-bottom", value=True)
stereo_mode_bt = gr.Checkbox(label="bottom-top", value=True)
stereo_mode_rl = gr.Checkbox(label="right-left", value=False)
stereo_mode_tb = gr.Checkbox(label="top-bottom", value=False)
stereo_mode_bt = gr.Checkbox(label="bottom-top", value=False)
stereo_mode_an = gr.Checkbox(label="red-cyan-anaglyph", value=True)


Expand Down Expand Up @@ -586,44 +586,27 @@ def run_depthmap(processed, outpath, inputimages, inputnames,

if gen_stereo:
print("Generating stereoscopic images..")

original_image = np.asarray(inputimages[count])
balance = (stereo_balance + 1) / 2
left_eye = original_image if balance < 0.001 else \
apply_stereo_divergence(original_image, img_output, -1 * stereo_divergence * (balance), stereo_fill)
right_eye = original_image if balance > 0.999 else \
apply_stereo_divergence(original_image, img_output, +1 * stereo_divergence * (1 - balance), stereo_fill)

stereoimages = []
stereonames = []
if stereo_mode_lr:
stereonames.append("left-right")
stereoimages.append(Image.fromarray(np.hstack([left_eye, right_eye])))
if stereo_mode_rl:
stereonames.append("right-left")
stereoimages.append(Image.fromarray(np.hstack([right_eye, left_eye])))
if stereo_mode_tb:
stereonames.append("top-bottom")
stereoimages.append(Image.fromarray(np.vstack([left_eye, right_eye])))
if stereo_mode_bt:
stereonames.append("bottom-top")
stereoimages.append(Image.fromarray(np.vstack([right_eye, left_eye])))
if stereo_mode_an:
stereonames.append("red-cyan-anaglyph")
stereoimages.append(Image.fromarray(overlap_red_cyan(left_eye, right_eye)))

stereomodes = []
if stereo_mode_lr: stereomodes.append("left-right")
if stereo_mode_rl: stereomodes.append("right-left")
if stereo_mode_tb: stereomodes.append("top-bottom")
if stereo_mode_bt: stereomodes.append("bottom-top")
if stereo_mode_an: stereomodes.append("red-cyan-anaglyph")
stereoimages = create_stereoimages(inputimages[count], img_output, stereo_divergence, stereomodes, stereo_balance, stereo_fill)

for c in range(0, len(stereoimages)):
outimages.append(stereoimages[c])
if processed is not None:
images.save_image(stereoimages[c], outpath, "", processed.all_seeds[count],
processed.all_prompts[count], opts.samples_format, info=info, p=processed,
suffix=f"_{stereonames[c]}")
suffix=f"_{stereomodes[c]}")
else:
# from tab
images.save_image(stereoimages[c], path=outpath, basename=basename, seed=None,
prompt=None, extension=opts.samples_format, info=info, short_filename=True,
no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=None,
forced_filename=None, suffix=f"_{stereonames[c]}")
forced_filename=None, suffix=f"_{stereomodes[c]}")

if gen_normal:
# taken from @graemeniedermayer, hidden, for api use only, will remove in future.
Expand Down
48 changes: 28 additions & 20 deletions scripts/stereoimage_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,51 @@
import numpy as np
from PIL import Image

def create_stereoimage(original_image, depthmap, divergence, mode='left-right', stereo_balance=0.0,
fill_technique='polylines_sharp'):
"""Creates a stereoscopic image.
An effort is made to make it look nice, but beware that the resulting image will have some distortion .
def create_stereoimages(original_image, depthmap, divergence, modes=None, stereo_balance=0.0,
fill_technique='polylines_sharp'):
"""Creates stereoscopic images.
An effort is made to make them look nice, but beware that the resulting image will have some distortion .

:param original_image: original image from which the 3D image (stereoimage) will be created
:param depthmap: depthmap corresponding to the original image
:param float divergence: the measure of 3D effect, in percentages.
A good value will likely be somewhere in the [0.05; 10.0) interval.
:param str mode: how the result will look like. The default is 'left-right'
--- a picture for the left eye will be on the left and the picture from the right eye --- on the rigth.
:param list modes: how the result will look like. By default only 'left-right' is generated
- a picture for the left eye will be on the left and the picture from the right eye - on the rigth.
The supported modes are: 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'red-cyan-anaglyph'.
:param float stereo_balance: has to do with how the divergence will be split among the two parts of the image,
must be in the [-1.0; 1.0] interval.
:param str fill_technique: applying divergence inevidably creates some gaps in the image.
This parameter specifies the technique that will be used to fill in the blanks in the two resulting images.
Must be one of the following: 'none', 'naive', 'naive_interpolating', 'polylines_soft', 'polylines_sharp'.
"""
if modes is None:
modes = ['left-right']
if not isinstance(modes, list):
modes = [modes]

original_image = np.asarray(original_image)
balance = (stereo_balance + 1) / 2
left_eye = original_image if balance < 0.001 else \
apply_stereo_divergence(original_image, depthmap, -1 * divergence * (balance), fill_technique)
apply_stereo_divergence(original_image, depthmap, -1 * divergence * balance, fill_technique)
right_eye = original_image if balance > 0.999 else \
apply_stereo_divergence(original_image, depthmap, +1 * divergence * (1 - balance), fill_technique)
if mode == 'left-right':
result = np.hstack([left_eye, right_eye])
elif mode == 'right-left':
result = np.hstack([right_eye, left_eye])
elif mode == 'top-bottom':
result = np.vstack([left_eye, right_eye])
elif mode == 'bottom-top':
result = np.vstack([right_eye, left_eye])
elif mode == 'red-cyan-anaglyph':
result = overlap_red_cyan(left_eye, right_eye)
else:
raise Exception('Unknown mode')
return Image.fromarray(result)

results = []
for mode in modes:
if mode == 'left-right':
results.append(np.hstack([left_eye, right_eye]))
elif mode == 'right-left':
results.append(np.hstack([right_eye, left_eye]))
elif mode == 'top-bottom':
results.append(np.vstack([left_eye, right_eye]))
elif mode == 'bottom-top':
results.append(np.vstack([right_eye, left_eye]))
elif mode == 'red-cyan-anaglyph':
results.append(overlap_red_cyan(left_eye, right_eye))
else:
raise Exception('Unknown mode')
return [Image.fromarray(r) for r in results]


def apply_stereo_divergence(original_image, depth, divergence, fill_technique):
Expand Down