Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Overbill1683
Stable Diffusion Webui
提交
db263df5
S
Stable Diffusion Webui
项目概览
Overbill1683
/
Stable Diffusion Webui
8 个月 前同步成功
通知
1703
Star
78
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
分析
仓库
DevOps
项目成员
Pages
S
Stable Diffusion Webui
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Pages
分析
分析
仓库分析
DevOps
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
提交
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
db263df5
编写于
4月 21, 2024
作者:
W
w-e-w
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
get_crop_region_v2
上级
d1998d74
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
44 addition
and
18 deletion
+44
-18
modules/masking.py
modules/masking.py
+32
-10
modules/processing.py
modules/processing.py
+12
-8
未找到文件。
modules/masking.py
浏览文件 @
db263df5
from
PIL
import
Image
,
ImageFilter
,
ImageOps
def
get_crop_region
(
mask
,
pad
=
0
):
"""finds a rectangular region that contains all masked ares in an image. Returns (x1, y1, x2, y2) coordinates of the rectangle.
For example, if a user has painted the top-right part of a 512x512 image, the result may be (256, 0, 512, 256)"""
mask_img
=
mask
if
isinstance
(
mask
,
Image
.
Image
)
else
Image
.
fromarray
(
mask
)
box
=
mask_img
.
getbbox
()
if
box
:
def
get_crop_region_v2
(
mask
,
pad
=
0
):
"""
Finds a rectangular region that contains all masked ares in a mask.
Returns None if mask is completely black mask (all 0)
Parameters:
mask: PIL.Image.Image L mode or numpy 1d array
pad: int number of pixels that the region will be extended on all sides
Returns: (x1, y1, x2, y2) | None
Introduced post 1.9.0
"""
mask
=
mask
if
isinstance
(
mask
,
Image
.
Image
)
else
Image
.
fromarray
(
mask
)
if
box
:
=
mask
.
getbbox
():
x1
,
y1
,
x2
,
y2
=
box
else
:
# when no box is found
x1
,
y1
=
mask_img
.
size
x2
=
y2
=
0
return
max
(
x1
-
pad
,
0
),
max
(
y1
-
pad
,
0
),
min
(
x2
+
pad
,
mask_img
.
size
[
0
]),
min
(
y2
+
pad
,
mask_img
.
size
[
1
])
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
def
get_crop_region
(
mask
,
pad
=
0
):
"""
Same function as get_crop_region_v2 but handles completely black mask (all 0) differently
when mask all black still return coordinates but the coordinates may be invalid ie x2>x1 or y2>y1
Notes: it is possible for the coordinates to be "valid" again if pad size is sufficiently large
(mask_size.x-pad, mask_size.y-pad, pad, pad)
Extension developer should use get_crop_region_v2 instead unless for compatibility considerations.
"""
mask
=
mask
if
isinstance
(
mask
,
Image
.
Image
)
else
Image
.
fromarray
(
mask
)
if
box
:
=
get_crop_region_v2
(
mask
,
pad
):
return
box
x1
,
y1
=
mask
.
size
x2
=
y2
=
0
return
max
(
x1
-
pad
,
0
),
max
(
y1
-
pad
,
0
),
min
(
x2
+
pad
,
mask
.
size
[
0
]),
min
(
y2
+
pad
,
mask
.
size
[
1
])
def
expand_crop_region
(
crop_region
,
processing_width
,
processing_height
,
image_width
,
image_height
):
...
...
modules/processing.py
浏览文件 @
db263df5
...
...
@@ -1611,19 +1611,23 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
if
self
.
inpaint_full_res
:
self
.
mask_for_overlay
=
image_mask
mask
=
image_mask
.
convert
(
'L'
)
crop_region
=
masking
.
get_crop_region
(
mask
,
self
.
inpaint_full_res_padding
)
if
crop_region
[
0
]
>=
crop_region
[
2
]
and
crop_region
[
1
]
>=
crop_region
[
3
]:
crop_region
=
None
image_mask
=
None
self
.
mask_for_overlay
=
None
else
:
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
)
x1
,
y1
,
x2
,
y2
=
crop_region
mask
=
mask
.
crop
(
crop_region
)
image_mask
=
images
.
resize_image
(
2
,
mask
,
self
.
width
,
self
.
height
)
self
.
inpaint_full_res
=
False
self
.
paste_to
=
(
x1
,
y1
,
x2
-
x1
,
y2
-
y1
)
self
.
extra_generation_params
[
"Inpaint area"
]
=
"Only masked"
self
.
extra_generation_params
[
"Masked area padding"
]
=
self
.
inpaint_full_res_padding
self
.
extra_generation_params
[
"Inpaint area"
]
=
"Only masked"
self
.
extra_generation_params
[
"Masked area padding"
]
=
self
.
inpaint_full_res_padding
else
:
crop_region
=
None
image_mask
=
None
self
.
mask_for_overlay
=
None
massage
=
'Unable to perform "Inpaint Only mask" because mask is blank, switch to img2img mode.'
model_hijack
.
comments
.
append
(
massage
)
logging
.
info
(
massage
)
else
:
image_mask
=
images
.
resize_image
(
self
.
resize_mode
,
image_mask
,
self
.
width
,
self
.
height
)
np_mask
=
np
.
array
(
image_mask
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录