Skip to content

Commit db263df

Browse files
committed
get_crop_region_v2
1 parent d1998d7 commit db263df

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

modules/masking.py

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
from PIL import Image, ImageFilter, ImageOps
22

33

4-
def get_crop_region(mask, pad=0):
5-
"""finds a rectangular region that contains all masked ares in an image. Returns (x1, y1, x2, y2) coordinates of the rectangle.
6-
For example, if a user has painted the top-right part of a 512x512 image, the result may be (256, 0, 512, 256)"""
7-
mask_img = mask if isinstance(mask, Image.Image) else Image.fromarray(mask)
8-
box = mask_img.getbbox()
9-
if box:
4+
def get_crop_region_v2(mask, pad=0):
5+
"""
6+
Finds a rectangular region that contains all masked ares in a mask.
7+
Returns None if mask is completely black mask (all 0)
8+
9+
Parameters:
10+
mask: PIL.Image.Image L mode or numpy 1d array
11+
pad: int number of pixels that the region will be extended on all sides
12+
Returns: (x1, y1, x2, y2) | None
13+
14+
Introduced post 1.9.0
15+
"""
16+
mask = mask if isinstance(mask, Image.Image) else Image.fromarray(mask)
17+
if box := mask.getbbox():
1018
x1, y1, x2, y2 = box
11-
else: # when no box is found
12-
x1, y1 = mask_img.size
13-
x2 = y2 = 0
14-
return max(x1 - pad, 0), max(y1 - pad, 0), min(x2 + pad, mask_img.size[0]), min(y2 + pad, mask_img.size[1])
19+
return max(x1 - pad, 0), max(y1 - pad, 0), min(x2 + pad, mask.size[0]), min(y2 + pad, mask.size[1]) if pad else box
20+
21+
22+
def get_crop_region(mask, pad=0):
23+
"""
24+
Same function as get_crop_region_v2 but handles completely black mask (all 0) differently
25+
when mask all black still return coordinates but the coordinates may be invalid ie x2>x1 or y2>y1
26+
Notes: it is possible for the coordinates to be "valid" again if pad size is sufficiently large
27+
(mask_size.x-pad, mask_size.y-pad, pad, pad)
28+
29+
Extension developer should use get_crop_region_v2 instead unless for compatibility considerations.
30+
"""
31+
mask = mask if isinstance(mask, Image.Image) else Image.fromarray(mask)
32+
if box := get_crop_region_v2(mask, pad):
33+
return box
34+
x1, y1 = mask.size
35+
x2 = y2 = 0
36+
return max(x1 - pad, 0), max(y1 - pad, 0), min(x2 + pad, mask.size[0]), min(y2 + pad, mask.size[1])
1537

1638

1739
def expand_crop_region(crop_region, processing_width, processing_height, image_width, image_height):

modules/processing.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1611,19 +1611,23 @@ def init(self, all_prompts, all_seeds, all_subseeds):
16111611
if self.inpaint_full_res:
16121612
self.mask_for_overlay = image_mask
16131613
mask = image_mask.convert('L')
1614-
crop_region = masking.get_crop_region(mask, self.inpaint_full_res_padding)
1615-
if crop_region[0] >= crop_region[2] and crop_region[1] >= crop_region[3]:
1616-
crop_region = None
1617-
image_mask = None
1618-
self.mask_for_overlay = None
1619-
else:
1614+
crop_region = masking.get_crop_region_v2(mask, self.inpaint_full_res_padding)
1615+
if crop_region:
16201616
crop_region = masking.expand_crop_region(crop_region, self.width, self.height, mask.width, mask.height)
16211617
x1, y1, x2, y2 = crop_region
16221618
mask = mask.crop(crop_region)
16231619
image_mask = images.resize_image(2, mask, self.width, self.height)
1620+
self.inpaint_full_res = False
16241621
self.paste_to = (x1, y1, x2-x1, y2-y1)
1625-
self.extra_generation_params["Inpaint area"] = "Only masked"
1626-
self.extra_generation_params["Masked area padding"] = self.inpaint_full_res_padding
1622+
self.extra_generation_params["Inpaint area"] = "Only masked"
1623+
self.extra_generation_params["Masked area padding"] = self.inpaint_full_res_padding
1624+
else:
1625+
crop_region = None
1626+
image_mask = None
1627+
self.mask_for_overlay = None
1628+
massage = 'Unable to perform "Inpaint Only mask" because mask is blank, switch to img2img mode.'
1629+
model_hijack.comments.append(massage)
1630+
logging.info(massage)
16271631
else:
16281632
image_mask = images.resize_image(self.resize_mode, image_mask, self.width, self.height)
16291633
np_mask = np.array(image_mask)

0 commit comments

Comments
 (0)