Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
8a6a4ad8
Commit
8a6a4ad8
authored
Jan 21, 2024
by
AUTOMATIC1111
Committed by
GitHub
Jan 21, 2024
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14709 from AUTOMATIC1111/improve-get_crop_region
improve get_crop_region
parents
3a5196de
e36827af
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
35 deletions
+10
-35
modules/masking.py
modules/masking.py
+9
-34
modules/processing.py
modules/processing.py
+1
-1
No files found.
modules/masking.py
View file @
8a6a4ad8
...
...
@@ -3,40 +3,15 @@ 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)"""
h
,
w
=
mask
.
shape
crop_left
=
0
for
i
in
range
(
w
):
if
not
(
mask
[:,
i
]
==
0
)
.
all
():
break
crop_left
+=
1
crop_right
=
0
for
i
in
reversed
(
range
(
w
)):
if
not
(
mask
[:,
i
]
==
0
)
.
all
():
break
crop_right
+=
1
crop_top
=
0
for
i
in
range
(
h
):
if
not
(
mask
[
i
]
==
0
)
.
all
():
break
crop_top
+=
1
crop_bottom
=
0
for
i
in
reversed
(
range
(
h
)):
if
not
(
mask
[
i
]
==
0
)
.
all
():
break
crop_bottom
+=
1
return
(
int
(
max
(
crop_left
-
pad
,
0
)),
int
(
max
(
crop_top
-
pad
,
0
)),
int
(
min
(
w
-
crop_right
+
pad
,
w
)),
int
(
min
(
h
-
crop_bottom
+
pad
,
h
))
)
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
:
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
])
def
expand_crop_region
(
crop_region
,
processing_width
,
processing_height
,
image_width
,
image_height
):
...
...
modules/processing.py
View file @
8a6a4ad8
...
...
@@ -1562,7 +1562,7 @@ 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
(
np
.
array
(
mask
)
,
self
.
inpaint_full_res_padding
)
crop_region
=
masking
.
get_crop_region
(
mask
,
self
.
inpaint_full_res_padding
)
crop_region
=
masking
.
expand_crop_region
(
crop_region
,
self
.
width
,
self
.
height
,
mask
.
width
,
mask
.
height
)
x1
,
y1
,
x2
,
y2
=
crop_region
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment