fix inpaint only masked

This commit is contained in:
Andray 2024-07-20 12:25:00 +04:00
parent 1da4907927
commit 0d7a17add5
3 changed files with 46 additions and 1 deletions

View File

@ -1,3 +1,4 @@
import math
from PIL import Image, ImageFilter, ImageOps
@ -77,6 +78,43 @@ def expand_crop_region(crop_region, processing_width, processing_height, image_w
return x1, y1, x2, y2
def fix_crop_region_integer_scale(crop_region, processing_width, processing_height, image_width, image_height):
"""expands crop region get_crop_region() to avoid non-integer scaling artifacts (different pixels size) after applying overlay"""
x1, y1, x2, y2 = crop_region
ratio_w = (x2 - x1) / processing_width
ratio_h = (y2 - y1) / processing_height
desired_w = math.ceil(ratio_w) * processing_width
diff_w = desired_w - (x2 - x1)
diff_w_l = diff_w // 2
diff_w_r = diff_w - diff_w_l
x1 -= diff_w_l
x2 += diff_w_r
if x1 < 0:
x2 -= x1
x1 -= x1
if x2 >= image_width:
x2 = image_width
desired_h = math.ceil(ratio_h) * processing_height
diff_h = desired_h - (y2 - y1)
diff_h_u = diff_h // 2
diff_h_d = diff_h - diff_h_u
y1 -= diff_h_u
y2 += diff_h_d
if y1 < 0:
y2 -= y1
y1 -= y1
if y2 >= image_height:
y2 = image_height
print(f"padding was increased by {max(diff_w_l, diff_w_r, diff_h_u, diff_h_d)} after integer upscale correction")
return x1, y1, x2, y2
def fill(image, mask):
"""fills masked regions with colors from image using blur. Not extremely effective."""

View File

@ -65,8 +65,12 @@ def apply_color_correction(correction, original_image):
def uncrop(image, dest_size, paste_loc):
x, y, w, h = paste_loc
base_image = Image.new('RGBA', dest_size)
factor_x = w // image.size[0]
factor_y = h // image.size[1]
image = images.resize_image(1, image, w, h)
base_image.paste(image, (x, y))
paste_x = max(x - factor_x, 0)
paste_y = max(y - factor_y, 0)
base_image.paste(image, (paste_x, paste_y))
image = base_image
return image
@ -1639,6 +1643,8 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
crop_region = masking.get_crop_region_v2(mask, self.inpaint_full_res_padding)
if crop_region:
crop_region = masking.expand_crop_region(crop_region, self.width, self.height, mask.width, mask.height)
if shared.opts.integer_only_masked:
crop_region = masking.fix_crop_region_integer_scale(crop_region, self.width, self.height, mask.width, mask.height)
x1, y1, x2, y2 = crop_region
mask = mask.crop(crop_region)
image_mask = images.resize_image(2, mask, self.width, self.height)

View File

@ -226,6 +226,7 @@ options_templates.update(options_section(('img2img', "img2img", "sd"), {
"return_mask_composite": OptionInfo(False, "For inpainting, include masked composite in results for web"),
"img2img_batch_show_results_limit": OptionInfo(32, "Show the first N batch img2img results in UI", gr.Slider, {"minimum": -1, "maximum": 1000, "step": 1}).info('0: disable, -1: show all images. Too many images can cause lag'),
"overlay_inpaint": OptionInfo(True, "Overlay original for inpaint").info("when inpainting, overlay the original image over the areas that weren't inpainted."),
"integer_only_masked": OptionInfo(False, "Integer upscale in inpaint only masked").info("Correct inpaint padding for only masked to have integer upscaling after fitting cropped region in original image"),
}))
options_templates.update(options_section(('optimizations', "Optimizations", "sd"), {