Skip to content

Commit 11971e8

Browse files
committed
add "Forbid too small crop region" option
1 parent 9f5a98d commit 11971e8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

modules/masking.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,44 @@ def expand_crop_region(crop_region, processing_width, processing_height, image_w
7777
return x1, y1, x2, y2
7878

7979

80+
def expand_too_small_crop_region(crop_region, processing_width, processing_height, image_width, image_height):
81+
"""expands crop region to not have width and height smaller then processing_width and processing_height"""
82+
83+
x1, y1, x2, y2 = crop_region
84+
85+
desired_w = processing_width
86+
diff_w = desired_w - (x2 - x1)
87+
if diff_w > 0:
88+
diff_w_l = diff_w // 2
89+
diff_w_r = diff_w - diff_w_l
90+
x1 -= diff_w_l
91+
x2 += diff_w_r
92+
if x1 < 0:
93+
x2 -= x1
94+
x1 -= x1
95+
if x2 >= image_width:
96+
x2 = image_width
97+
98+
desired_h = processing_height
99+
diff_h = desired_h - (y2 - y1)
100+
if diff_h > 0:
101+
diff_h_u = diff_h // 2
102+
diff_h_d = diff_h - diff_h_u
103+
y1 -= diff_h_u
104+
y2 += diff_h_d
105+
if y1 < 0:
106+
y2 -= y1
107+
y1 -= y1
108+
if y2 >= image_height:
109+
y2 = image_height
110+
111+
if diff_h > 0 or diff_w > 0:
112+
print("Crop region was smaller then resolution and has been corrected")
113+
114+
return x1, y1, x2, y2
115+
116+
117+
80118
def fill(image, mask):
81119
"""fills masked regions with colors from image using blur. Not extremely effective."""
82120

modules/processing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,8 @@ def init(self, all_prompts, all_seeds, all_subseeds):
16391639
crop_region = masking.get_crop_region_v2(mask, self.inpaint_full_res_padding)
16401640
if crop_region:
16411641
crop_region = masking.expand_crop_region(crop_region, self.width, self.height, mask.width, mask.height)
1642+
if shared.opts.forbid_too_small_crop_region:
1643+
crop_region = masking.expand_too_small_crop_region(crop_region, self.width, self.height, mask.width, mask.height)
16421644
x1, y1, x2, y2 = crop_region
16431645
mask = mask.crop(crop_region)
16441646
image_mask = images.resize_image(2, mask, self.width, self.height)

modules/shared_options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@
227227
"return_mask_composite": OptionInfo(False, "For inpainting, include masked composite in results for web"),
228228
"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'),
229229
"overlay_inpaint": OptionInfo(True, "Overlay original for inpaint").info("when inpainting, overlay the original image over the areas that weren't inpainted."),
230+
"forbid_too_small_crop_region": OptionInfo(False, "Forbid too small crop region").info("Correct inpaint padding for only masked to avoid crop region sides less then processing resolution"),
230231
}))
231232

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

0 commit comments

Comments
 (0)