2022-09-03 03:08:45 -06:00
|
|
|
import math
|
2022-09-22 03:11:48 -06:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2022-09-07 20:35:26 -06:00
|
|
|
import numpy as np
|
2022-09-07 14:37:54 -06:00
|
|
|
from PIL import Image, ImageOps, ImageChops
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-11 14:24:24 -06:00
|
|
|
from modules import devices
|
2022-09-03 03:08:45 -06:00
|
|
|
from modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images
|
|
|
|
from modules.shared import opts, state
|
|
|
|
import modules.shared as shared
|
|
|
|
import modules.processing as processing
|
|
|
|
from modules.ui import plaintext_to_html
|
|
|
|
import modules.images as images
|
2022-09-03 08:21:15 -06:00
|
|
|
import modules.scripts
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-22 03:11:48 -06:00
|
|
|
|
|
|
|
def process_batch(p, input_dir, output_dir, args):
|
|
|
|
processing.fix_seed(p)
|
|
|
|
|
2022-10-28 23:11:03 -06:00
|
|
|
images = shared.listfiles(input_dir)
|
2022-09-22 03:11:48 -06:00
|
|
|
|
|
|
|
print(f"Will process {len(images)} images, creating {p.n_iter * p.batch_size} new images for each.")
|
|
|
|
|
2022-10-03 02:48:19 -06:00
|
|
|
save_normally = output_dir == ''
|
|
|
|
|
2022-09-22 03:11:48 -06:00
|
|
|
p.do_not_save_grid = True
|
2022-10-03 02:48:19 -06:00
|
|
|
p.do_not_save_samples = not save_normally
|
2022-09-22 03:11:48 -06:00
|
|
|
|
|
|
|
state.job_count = len(images) * p.n_iter
|
|
|
|
|
|
|
|
for i, image in enumerate(images):
|
|
|
|
state.job = f"{i+1} out of {len(images)}"
|
2022-10-04 21:56:30 -06:00
|
|
|
if state.skipped:
|
|
|
|
state.skipped = False
|
2022-09-22 03:11:48 -06:00
|
|
|
|
|
|
|
if state.interrupted:
|
|
|
|
break
|
|
|
|
|
|
|
|
img = Image.open(image)
|
2022-10-25 02:39:59 -06:00
|
|
|
# Use the EXIF orientation of photos taken by smartphones.
|
|
|
|
img = ImageOps.exif_transpose(img)
|
2022-09-22 03:11:48 -06:00
|
|
|
p.init_images = [img] * p.batch_size
|
|
|
|
|
|
|
|
proc = modules.scripts.scripts_img2img.run(p, *args)
|
|
|
|
if proc is None:
|
|
|
|
proc = process_images(p)
|
|
|
|
|
|
|
|
for n, processed_image in enumerate(proc.images):
|
|
|
|
filename = os.path.basename(image)
|
|
|
|
|
|
|
|
if n > 0:
|
|
|
|
left, right = os.path.splitext(filename)
|
|
|
|
filename = f"{left}-{n}{right}"
|
|
|
|
|
2022-10-03 02:48:19 -06:00
|
|
|
if not save_normally:
|
2022-10-31 18:40:54 -06:00
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
2022-10-03 02:48:19 -06:00
|
|
|
processed_image.save(os.path.join(output_dir, filename))
|
2022-09-22 03:11:48 -06:00
|
|
|
|
|
|
|
|
2022-11-07 17:58:49 -07:00
|
|
|
def img2img(mode: int, prompt: str, negative_prompt: str, prompt_style: str, prompt_style2: str, init_img, init_img_with_mask, init_img_with_mask_orig, init_img_inpaint, init_mask_inpaint, mask_mode, steps: int, sampler_index: int, mask_blur: int, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, denoising_strength: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, height: int, width: int, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, *args):
|
2022-09-03 03:08:45 -06:00
|
|
|
is_inpaint = mode == 1
|
2022-09-22 03:11:48 -06:00
|
|
|
is_batch = mode == 2
|
2022-09-03 03:08:45 -06:00
|
|
|
|
|
|
|
if is_inpaint:
|
2022-10-25 02:39:59 -06:00
|
|
|
# Drawn mask
|
2022-09-09 10:43:16 -06:00
|
|
|
if mask_mode == 0:
|
2022-11-07 17:58:49 -07:00
|
|
|
image = init_img_with_mask
|
|
|
|
is_mask_sketch = isinstance(image, dict)
|
|
|
|
if is_mask_sketch:
|
|
|
|
# Sketch: mask iff. not transparent
|
|
|
|
image, mask = image["image"], image["mask"]
|
|
|
|
mask = np.array(mask)[..., -1] > 0
|
|
|
|
else:
|
|
|
|
# Color-sketch: mask iff. painted over
|
|
|
|
orig = init_img_with_mask_orig or image
|
|
|
|
mask = np.any(np.array(image) != np.array(orig), axis=-1)
|
|
|
|
mask = Image.fromarray(mask.astype(np.uint8) * 255, "L")
|
|
|
|
image = image.convert("RGB")
|
2022-10-25 02:39:59 -06:00
|
|
|
# Uploaded mask
|
2022-09-09 10:43:16 -06:00
|
|
|
else:
|
2022-09-22 03:11:48 -06:00
|
|
|
image = init_img_inpaint
|
|
|
|
mask = init_mask_inpaint
|
2022-10-25 02:39:59 -06:00
|
|
|
# No mask
|
2022-09-03 03:08:45 -06:00
|
|
|
else:
|
|
|
|
image = init_img
|
|
|
|
mask = None
|
|
|
|
|
2022-10-25 02:39:59 -06:00
|
|
|
# Use the EXIF orientation of photos taken by smartphones.
|
2022-10-26 06:32:55 -06:00
|
|
|
if image is not None:
|
|
|
|
image = ImageOps.exif_transpose(image)
|
2022-10-25 02:39:59 -06:00
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'
|
|
|
|
|
|
|
|
p = StableDiffusionProcessingImg2Img(
|
|
|
|
sd_model=shared.sd_model,
|
|
|
|
outpath_samples=opts.outdir_samples or opts.outdir_img2img_samples,
|
|
|
|
outpath_grids=opts.outdir_grids or opts.outdir_img2img_grids,
|
|
|
|
prompt=prompt,
|
2022-09-09 00:15:36 -06:00
|
|
|
negative_prompt=negative_prompt,
|
2022-09-14 08:56:21 -06:00
|
|
|
styles=[prompt_style, prompt_style2],
|
2022-09-03 03:08:45 -06:00
|
|
|
seed=seed,
|
2022-09-09 08:54:04 -06:00
|
|
|
subseed=subseed,
|
|
|
|
subseed_strength=subseed_strength,
|
|
|
|
seed_resize_from_h=seed_resize_from_h,
|
|
|
|
seed_resize_from_w=seed_resize_from_w,
|
2022-09-21 04:34:10 -06:00
|
|
|
seed_enable_extras=seed_enable_extras,
|
2022-09-03 03:08:45 -06:00
|
|
|
sampler_index=sampler_index,
|
|
|
|
batch_size=batch_size,
|
|
|
|
n_iter=n_iter,
|
|
|
|
steps=steps,
|
|
|
|
cfg_scale=cfg_scale,
|
|
|
|
width=width,
|
|
|
|
height=height,
|
2022-09-07 03:32:28 -06:00
|
|
|
restore_faces=restore_faces,
|
2022-09-04 18:25:37 -06:00
|
|
|
tiling=tiling,
|
2022-09-03 03:08:45 -06:00
|
|
|
init_images=[image],
|
|
|
|
mask=mask,
|
|
|
|
mask_blur=mask_blur,
|
|
|
|
inpainting_fill=inpainting_fill,
|
|
|
|
resize_mode=resize_mode,
|
|
|
|
denoising_strength=denoising_strength,
|
|
|
|
inpaint_full_res=inpaint_full_res,
|
2022-09-22 03:11:48 -06:00
|
|
|
inpaint_full_res_padding=inpaint_full_res_padding,
|
2022-09-03 12:02:38 -06:00
|
|
|
inpainting_mask_invert=inpainting_mask_invert,
|
2022-09-03 03:08:45 -06:00
|
|
|
)
|
2022-10-02 11:23:40 -06:00
|
|
|
|
2022-10-22 03:23:45 -06:00
|
|
|
p.scripts = modules.scripts.scripts_txt2img
|
|
|
|
p.script_args = args
|
2022-10-16 09:53:56 -06:00
|
|
|
|
2022-10-02 11:23:40 -06:00
|
|
|
if shared.cmd_opts.enable_console_prompts:
|
|
|
|
print(f"\nimg2img: {prompt}", file=shared.progress_print_out)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-20 10:07:09 -06:00
|
|
|
p.extra_generation_params["Mask blur"] = mask_blur
|
|
|
|
|
2022-09-22 03:11:48 -06:00
|
|
|
if is_batch:
|
2022-09-24 07:29:20 -06:00
|
|
|
assert not shared.cmd_opts.hide_ui_dir_config, "Launched with --hide-ui-dir-config, batch img2img disabled"
|
|
|
|
|
2022-09-22 03:11:48 -06:00
|
|
|
process_batch(p, img2img_batch_input_dir, img2img_batch_output_dir, args)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-22 03:11:48 -06:00
|
|
|
processed = Processed(p, [], p.seed, "")
|
2022-09-03 03:08:45 -06:00
|
|
|
else:
|
2022-09-03 16:29:43 -06:00
|
|
|
processed = modules.scripts.scripts_img2img.run(p, *args)
|
2022-09-03 08:21:15 -06:00
|
|
|
if processed is None:
|
|
|
|
processed = process_images(p)
|
|
|
|
|
2022-11-01 18:56:47 -06:00
|
|
|
p.close()
|
|
|
|
|
2022-09-08 07:37:13 -06:00
|
|
|
shared.total_tqdm.clear()
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-22 02:47:16 -06:00
|
|
|
generation_info_js = processed.js()
|
|
|
|
if opts.samples_log_stdout:
|
|
|
|
print(generation_info_js)
|
|
|
|
|
2022-10-04 08:23:48 -06:00
|
|
|
if opts.do_not_show_images:
|
|
|
|
processed.images = []
|
|
|
|
|
2022-09-28 09:20:30 -06:00
|
|
|
return processed.images, generation_info_js, plaintext_to_html(processed.info)
|