Skip to content

Commit c21be96

Browse files
committed
add threshold; remove integer_only_masked
1 parent 0d7a17a commit c21be96

File tree

3 files changed

+12
-45
lines changed

3 files changed

+12
-45
lines changed

modules/masking.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import math
21
from PIL import Image, ImageFilter, ImageOps
32

43

@@ -78,43 +77,6 @@ def expand_crop_region(crop_region, processing_width, processing_height, image_w
7877
return x1, y1, x2, y2
7978

8079

81-
def fix_crop_region_integer_scale(crop_region, processing_width, processing_height, image_width, image_height):
82-
"""expands crop region get_crop_region() to avoid non-integer scaling artifacts (different pixels size) after applying overlay"""
83-
84-
x1, y1, x2, y2 = crop_region
85-
86-
ratio_w = (x2 - x1) / processing_width
87-
ratio_h = (y2 - y1) / processing_height
88-
89-
desired_w = math.ceil(ratio_w) * processing_width
90-
diff_w = desired_w - (x2 - x1)
91-
diff_w_l = diff_w // 2
92-
diff_w_r = diff_w - diff_w_l
93-
x1 -= diff_w_l
94-
x2 += diff_w_r
95-
if x1 < 0:
96-
x2 -= x1
97-
x1 -= x1
98-
if x2 >= image_width:
99-
x2 = image_width
100-
101-
desired_h = math.ceil(ratio_h) * processing_height
102-
diff_h = desired_h - (y2 - y1)
103-
diff_h_u = diff_h // 2
104-
diff_h_d = diff_h - diff_h_u
105-
y1 -= diff_h_u
106-
y2 += diff_h_d
107-
if y1 < 0:
108-
y2 -= y1
109-
y1 -= y1
110-
if y2 >= image_height:
111-
y2 = image_height
112-
113-
print(f"padding was increased by {max(diff_w_l, diff_w_r, diff_h_u, diff_h_d)} after integer upscale correction")
114-
115-
return x1, y1, x2, y2
116-
117-
11880
def fill(image, mask):
11981
"""fills masked regions with colors from image using blur. Not extremely effective."""
12082

modules/processing.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,18 @@ def apply_color_correction(correction, original_image):
6565
def uncrop(image, dest_size, paste_loc):
6666
x, y, w, h = paste_loc
6767
base_image = Image.new('RGBA', dest_size)
68-
factor_x = w // image.size[0]
69-
factor_y = h // image.size[1]
68+
69+
if image.width > shared.opts.img2img_inpaint_correct_paste_xy_side_length_threshold:
70+
paste_x = max(x - w // image.width, 0)
71+
else:
72+
paste_x = x
73+
74+
if image.height > shared.opts.img2img_inpaint_correct_paste_xy_side_length_threshold:
75+
paste_y = max(y - h // image.height, 0)
76+
else:
77+
paste_y = y
78+
7079
image = images.resize_image(1, image, w, h)
71-
paste_x = max(x - factor_x, 0)
72-
paste_y = max(y - factor_y, 0)
7380
base_image.paste(image, (paste_x, paste_y))
7481
image = base_image
7582

@@ -1643,8 +1650,6 @@ def init(self, all_prompts, all_seeds, all_subseeds):
16431650
crop_region = masking.get_crop_region_v2(mask, self.inpaint_full_res_padding)
16441651
if crop_region:
16451652
crop_region = masking.expand_crop_region(crop_region, self.width, self.height, mask.width, mask.height)
1646-
if shared.opts.integer_only_masked:
1647-
crop_region = masking.fix_crop_region_integer_scale(crop_region, self.width, self.height, mask.width, mask.height)
16481653
x1, y1, x2, y2 = crop_region
16491654
mask = mask.crop(crop_region)
16501655
image_mask = images.resize_image(2, mask, self.width, self.height)

modules/shared_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226
"return_mask_composite": OptionInfo(False, "For inpainting, include masked composite in results for web"),
227227
"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'),
228228
"overlay_inpaint": OptionInfo(True, "Overlay original for inpaint").info("when inpainting, overlay the original image over the areas that weren't inpainted."),
229-
"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"),
229+
"img2img_inpaint_correct_paste_xy_side_length_threshold": OptionInfo(400, "If a side of generation resolution is bigger then the threshold, paste_xy will be corrected in only masked mode to fix shifting. The bigger resolution means less details are lost in downscaling step", gr.Slider, {"minimum": 0, "maximum": 1000, "step": 1}),
230230
}))
231231

232232
options_templates.update(options_section(('optimizations', "Optimizations", "sd"), {

0 commit comments

Comments
 (0)