2022-09-22 12:11:48 +03:00
|
|
|
import os
|
2023-05-29 19:39:24 +03:00
|
|
|
from pathlib import Path
|
2022-09-22 12:11:48 +03:00
|
|
|
|
2022-09-08 05:35:26 +03:00
|
|
|
import numpy as np
|
2023-03-31 11:54:42 +03:00
|
|
|
from PIL import Image, ImageOps, ImageFilter, ImageEnhance, ImageChops, UnidentifiedImageError
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2023-05-10 08:43:42 +03:00
|
|
|
from modules import sd_samplers
|
2023-01-30 02:40:26 +03:00
|
|
|
from modules.generation_parameters_copypaste import create_override_settings_dict
|
2022-09-03 12:08:45 +03: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
|
2022-09-03 17:21:15 +03:00
|
|
|
import modules.scripts
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-09-22 12:11:48 +03:00
|
|
|
|
2023-05-29 19:47:20 +03:00
|
|
|
def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args, to_scale=False, scale_by=1.0):
|
2022-09-22 12:11:48 +03:00
|
|
|
processing.fix_seed(p)
|
|
|
|
|
2022-10-29 08:11:03 +03:00
|
|
|
images = shared.listfiles(input_dir)
|
2022-09-22 12:11:48 +03:00
|
|
|
|
2023-01-28 16:42:24 +03:00
|
|
|
is_inpaint_batch = False
|
|
|
|
if inpaint_mask_dir:
|
|
|
|
inpaint_masks = shared.listfiles(inpaint_mask_dir)
|
|
|
|
is_inpaint_batch = len(inpaint_masks) > 0
|
2023-01-28 04:32:31 +03:00
|
|
|
if is_inpaint_batch:
|
|
|
|
print(f"\nInpaint batch is enabled. {len(inpaint_masks)} masks found.")
|
|
|
|
|
2022-09-22 12:11:48 +03:00
|
|
|
print(f"Will process {len(images)} images, creating {p.n_iter * p.batch_size} new images for each.")
|
|
|
|
|
2022-10-03 11:48:19 +03:00
|
|
|
save_normally = output_dir == ''
|
|
|
|
|
2022-09-22 12:11:48 +03:00
|
|
|
p.do_not_save_grid = True
|
2022-10-03 11:48:19 +03:00
|
|
|
p.do_not_save_samples = not save_normally
|
2022-09-22 12:11:48 +03: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-05 06:56:30 +03:00
|
|
|
if state.skipped:
|
|
|
|
state.skipped = False
|
2022-09-22 12:11:48 +03:00
|
|
|
|
|
|
|
if state.interrupted:
|
|
|
|
break
|
|
|
|
|
2023-03-31 11:54:42 +03:00
|
|
|
try:
|
|
|
|
img = Image.open(image)
|
2023-05-03 08:28:59 +03:00
|
|
|
except UnidentifiedImageError as e:
|
|
|
|
print(e)
|
2023-03-31 11:54:42 +03:00
|
|
|
continue
|
2022-10-25 11:39:59 +03:00
|
|
|
# Use the EXIF orientation of photos taken by smartphones.
|
2022-11-09 06:06:29 +03:00
|
|
|
img = ImageOps.exif_transpose(img)
|
2023-05-29 20:38:49 +03:00
|
|
|
|
2023-05-29 19:47:20 +03:00
|
|
|
if to_scale:
|
|
|
|
p.width = int(img.width * scale_by)
|
|
|
|
p.height = int(img.height * scale_by)
|
2023-05-29 20:38:49 +03:00
|
|
|
|
2022-09-22 12:11:48 +03:00
|
|
|
p.init_images = [img] * p.batch_size
|
|
|
|
|
2023-06-01 10:29:56 +03:00
|
|
|
image_path = Path(image)
|
2023-01-28 04:32:31 +03:00
|
|
|
if is_inpaint_batch:
|
|
|
|
# try to find corresponding mask for an image using simple filename matching
|
2023-06-01 10:29:56 +03:00
|
|
|
if len(inpaint_masks) == 1:
|
2023-01-28 04:32:31 +03:00
|
|
|
mask_image_path = inpaint_masks[0]
|
2023-06-01 10:29:56 +03:00
|
|
|
else:
|
|
|
|
# try to find corresponding mask for an image using simple filename matching
|
2023-06-01 14:44:55 +03:00
|
|
|
mask_image_dir = Path(inpaint_mask_dir)
|
2023-06-01 10:29:56 +03:00
|
|
|
masks_found = list(mask_image_dir.glob(f"{image_path.stem}.*"))
|
|
|
|
|
|
|
|
if len(masks_found) == 0:
|
2023-06-01 14:44:55 +03:00
|
|
|
print(f"Warning: mask is not found for {image_path} in {mask_image_dir}. Skipping it.")
|
|
|
|
continue
|
2023-06-01 10:29:56 +03:00
|
|
|
|
|
|
|
# it should contain only 1 matching mask
|
|
|
|
# otherwise user has many masks with the same name but different extensions
|
|
|
|
mask_image_path = masks_found[0]
|
|
|
|
|
2023-01-28 04:32:31 +03:00
|
|
|
mask_image = Image.open(mask_image_path)
|
|
|
|
p.image_mask = mask_image
|
|
|
|
|
2022-09-22 12:11:48 +03:00
|
|
|
proc = modules.scripts.scripts_img2img.run(p, *args)
|
|
|
|
if proc is None:
|
|
|
|
proc = process_images(p)
|
|
|
|
|
|
|
|
for n, processed_image in enumerate(proc.images):
|
2023-06-01 10:29:56 +03:00
|
|
|
filename = image_path.name
|
2022-09-22 12:11:48 +03:00
|
|
|
|
|
|
|
if n > 0:
|
|
|
|
left, right = os.path.splitext(filename)
|
|
|
|
filename = f"{left}-{n}{right}"
|
|
|
|
|
2022-10-03 11:48:19 +03:00
|
|
|
if not save_normally:
|
2022-11-01 03:40:54 +03:00
|
|
|
os.makedirs(output_dir, exist_ok=True)
|
2023-02-08 15:03:36 +03:00
|
|
|
if processed_image.mode == 'RGBA':
|
|
|
|
processed_image = processed_image.convert("RGB")
|
2022-10-03 11:48:19 +03:00
|
|
|
processed_image.save(os.path.join(output_dir, filename))
|
2022-09-22 12:11:48 +03:00
|
|
|
|
|
|
|
|
2023-03-28 22:23:40 +03:00
|
|
|
def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_styles, init_img, sketch, init_img_with_mask, inpaint_color_sketch, inpaint_color_sketch_orig, init_img_inpaint, init_mask_inpaint, steps: int, sampler_index: int, mask_blur: int, mask_alpha: float, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, image_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, selected_scale_tab: int, height: int, width: int, scale_by: float, 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, img2img_batch_inpaint_mask_dir: str, override_settings_texts, *args):
|
2023-01-30 02:40:26 +03:00
|
|
|
override_settings = create_override_settings_dict(override_settings_texts)
|
|
|
|
|
2023-01-11 20:33:24 +03:00
|
|
|
is_batch = mode == 5
|
|
|
|
|
|
|
|
if mode == 0: # img2img
|
|
|
|
image = init_img.convert("RGB")
|
|
|
|
mask = None
|
|
|
|
elif mode == 1: # img2img sketch
|
|
|
|
image = sketch.convert("RGB")
|
|
|
|
mask = None
|
|
|
|
elif mode == 2: # inpaint
|
|
|
|
image, mask = init_img_with_mask["image"], init_img_with_mask["mask"]
|
|
|
|
alpha_mask = ImageOps.invert(image.split()[-1]).convert('L').point(lambda x: 255 if x > 0 else 0, mode='1')
|
2023-05-22 22:56:26 +03:00
|
|
|
mask = mask.convert('L').point(lambda x: 255 if x > 128 else 0, mode='1')
|
|
|
|
mask = ImageChops.lighter(alpha_mask, mask).convert('L')
|
2023-01-11 20:33:24 +03:00
|
|
|
image = image.convert("RGB")
|
|
|
|
elif mode == 3: # inpaint sketch
|
|
|
|
image = inpaint_color_sketch
|
|
|
|
orig = inpaint_color_sketch_orig or inpaint_color_sketch
|
|
|
|
pred = np.any(np.array(image) != np.array(orig), axis=-1)
|
|
|
|
mask = Image.fromarray(pred.astype(np.uint8) * 255, "L")
|
|
|
|
mask = ImageEnhance.Brightness(mask).enhance(1 - mask_alpha / 100)
|
|
|
|
blur = ImageFilter.GaussianBlur(mask_blur)
|
|
|
|
image = Image.composite(image.filter(blur), orig, mask.filter(blur))
|
|
|
|
image = image.convert("RGB")
|
|
|
|
elif mode == 4: # inpaint upload mask
|
|
|
|
image = init_img_inpaint
|
|
|
|
mask = init_mask_inpaint
|
2022-09-03 12:08:45 +03:00
|
|
|
else:
|
2023-01-11 20:33:24 +03:00
|
|
|
image = None
|
2022-09-03 12:08:45 +03:00
|
|
|
mask = None
|
|
|
|
|
2022-10-25 11:39:59 +03:00
|
|
|
# Use the EXIF orientation of photos taken by smartphones.
|
2022-10-26 15:32:55 +03:00
|
|
|
if image is not None:
|
2022-11-09 06:06:29 +03:00
|
|
|
image = ImageOps.exif_transpose(image)
|
2022-10-25 11:39:59 +03:00
|
|
|
|
2023-05-29 19:47:20 +03:00
|
|
|
if selected_scale_tab == 1 and not is_batch:
|
2023-03-28 22:23:40 +03:00
|
|
|
assert image, "Can't scale by because no image is selected"
|
|
|
|
|
|
|
|
width = int(image.width * scale_by)
|
|
|
|
height = int(image.height * scale_by)
|
|
|
|
|
2022-09-03 12:08:45 +03: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 09:15:36 +03:00
|
|
|
negative_prompt=negative_prompt,
|
2023-01-14 14:56:39 +03:00
|
|
|
styles=prompt_styles,
|
2022-09-03 12:08:45 +03:00
|
|
|
seed=seed,
|
2022-09-09 17:54:04 +03: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 13:34:10 +03:00
|
|
|
seed_enable_extras=seed_enable_extras,
|
2022-11-27 13:17:39 +03:00
|
|
|
sampler_name=sd_samplers.samplers_for_img2img[sampler_index].name,
|
2022-09-03 12:08:45 +03:00
|
|
|
batch_size=batch_size,
|
|
|
|
n_iter=n_iter,
|
|
|
|
steps=steps,
|
|
|
|
cfg_scale=cfg_scale,
|
|
|
|
width=width,
|
|
|
|
height=height,
|
2022-09-07 12:32:28 +03:00
|
|
|
restore_faces=restore_faces,
|
2022-09-05 03:25:37 +03:00
|
|
|
tiling=tiling,
|
2022-09-03 12:08:45 +03:00
|
|
|
init_images=[image],
|
|
|
|
mask=mask,
|
|
|
|
mask_blur=mask_blur,
|
|
|
|
inpainting_fill=inpainting_fill,
|
|
|
|
resize_mode=resize_mode,
|
|
|
|
denoising_strength=denoising_strength,
|
2023-02-04 02:19:56 +03:00
|
|
|
image_cfg_scale=image_cfg_scale,
|
2022-09-03 12:08:45 +03:00
|
|
|
inpaint_full_res=inpaint_full_res,
|
2022-09-22 12:11:48 +03:00
|
|
|
inpaint_full_res_padding=inpaint_full_res_padding,
|
2022-09-03 21:02:38 +03:00
|
|
|
inpainting_mask_invert=inpainting_mask_invert,
|
2023-01-30 02:40:26 +03:00
|
|
|
override_settings=override_settings,
|
2022-09-03 12:08:45 +03:00
|
|
|
)
|
2022-10-02 20:23:40 +03:00
|
|
|
|
2023-04-24 12:36:16 +03:00
|
|
|
p.scripts = modules.scripts.scripts_img2img
|
2022-10-22 12:23:45 +03:00
|
|
|
p.script_args = args
|
2022-10-16 18:53:56 +03:00
|
|
|
|
2022-10-02 20:23:40 +03:00
|
|
|
if shared.cmd_opts.enable_console_prompts:
|
|
|
|
print(f"\nimg2img: {prompt}", file=shared.progress_print_out)
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2023-03-28 22:20:31 +03:00
|
|
|
if mask:
|
|
|
|
p.extra_generation_params["Mask blur"] = mask_blur
|
2022-09-20 19:07:09 +03:00
|
|
|
|
2022-09-22 12:11:48 +03:00
|
|
|
if is_batch:
|
2022-09-24 16:29:20 +03:00
|
|
|
assert not shared.cmd_opts.hide_ui_dir_config, "Launched with --hide-ui-dir-config, batch img2img disabled"
|
|
|
|
|
2023-05-29 19:47:20 +03:00
|
|
|
process_batch(p, img2img_batch_input_dir, img2img_batch_output_dir, img2img_batch_inpaint_mask_dir, args, to_scale=selected_scale_tab == 1, scale_by=scale_by)
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-09-22 12:11:48 +03:00
|
|
|
processed = Processed(p, [], p.seed, "")
|
2022-09-03 12:08:45 +03:00
|
|
|
else:
|
2022-09-04 01:29:43 +03:00
|
|
|
processed = modules.scripts.scripts_img2img.run(p, *args)
|
2022-09-03 17:21:15 +03:00
|
|
|
if processed is None:
|
|
|
|
processed = process_images(p)
|
|
|
|
|
2022-11-02 03:56:47 +03:00
|
|
|
p.close()
|
|
|
|
|
2022-09-08 16:37:13 +03:00
|
|
|
shared.total_tqdm.clear()
|
2022-09-03 12:08:45 +03:00
|
|
|
|
2022-09-22 11:47:16 +03:00
|
|
|
generation_info_js = processed.js()
|
|
|
|
if opts.samples_log_stdout:
|
|
|
|
print(generation_info_js)
|
|
|
|
|
2022-10-04 17:23:48 +03:00
|
|
|
if opts.do_not_show_images:
|
|
|
|
processed.images = []
|
|
|
|
|
2022-12-31 23:40:55 +03:00
|
|
|
return processed.images, generation_info_js, plaintext_to_html(processed.info), plaintext_to_html(processed.comments)
|