use box for inoutpaint

pull/35/head
Kohaku-Blueleaf 2024-01-20 14:41:44 +08:00
parent 90ec2efc97
commit 52e38efc90
3 changed files with 49 additions and 25 deletions

View File

@ -13,25 +13,27 @@ except:
HTML_TEMPLATES = {"resolution": """<textarea>"""}
def run(img, u, d, l, r, mode="fill"):
def run(img, w, h, t, b, l, r, mode="fill"):
w, h, t, b, l, r = int(w), int(h), int(t), int(b), int(l), int(r)
img = img.resize((r-l, b-t))
new_img, mask = resize_with_mask(
img,
(u, d, l, r),
img, w, h, t, b, l, r
)
w, h = img.size
w_n, h_n = new_img.size
if u:
new_img.paste(img.resize((w, u), box=(0, 0, w, 0)), box=(l, 0))
if d:
new_img.paste(img.resize((w, d), box=(0, h, w, h)), box=(l, u + h))
if t:
new_img.paste(img.resize((w, t), box=(0, 0, w, 0)), box=(l, 0))
if b<h_n:
new_img.paste(img.resize((w, h_n-b), box=(0, h, w, h)), box=(l, b))
if l:
new_img.paste(new_img.resize((l, h_n), box=(l + 1, 0, l + 1, h_n)), box=(0, 0))
if r:
if r<w_n:
new_img.paste(
new_img.resize((r, h_n), box=(w + l - 1, 0, w + l - 1, h_n)), box=(l + w, 0)
new_img.resize((w_n-r, h_n), box=(w + l - 1, 0, w + l - 1, h_n)), box=(l + w, 0)
)
return new_img, mask, f"{w_n} x {h_n}"

View File

@ -6,15 +6,12 @@ from PIL import Image
def resize_with_mask(
img: Image.Image, expands: tuple[int, int, int, int]
img: Image.Image, w, h, t, b, l, r,
) -> tuple[Image.Image, Image.Image]:
w, h = img.size
u, d, l, r = expands
new_img = Image.new("RGB", (w, h))
new_img.paste(img, (l, t))
new_img = Image.new("RGB", (w + l + r, h + u + d))
new_img.paste(img, (l, u))
mask = Image.new("L", (w + l + r, h + u + d), 255)
mask_none = Image.new("L", (w, h), 0)
mask.paste(mask_none, (l, u))
mask = Image.new("L", (w, h), 255)
mask_none = Image.new("L", (r-l, b-t), 0)
mask.paste(mask_none, (l, t))
return new_img, mask

View File

@ -357,12 +357,30 @@ def add_tab():
)
with gr.Tabs(elem_id="function list"):
with gr.TabItem("InOutPaint"):
iop_u = gr.Slider(0, 512, 0, step=64, label="fill up")
iop_d = gr.Slider(0, 512, 0, step=64, label="fill down")
iop_l = gr.Slider(0, 512, 0, step=64, label="fill left")
iop_r = gr.Slider(
0, 512, 0, step=64, label="fill right"
)
with gr.Row():
with gr.Column():
iop_width = gr.Number(
value=512, label="output width"
)
with gr.Column():
iop_height = gr.Number(
value=512, label="output Height"
)
with gr.Row():
with gr.Column():
iop_align_top = gr.Number(
value=0, label="align top"
)
iop_align_left = gr.Number(
value=0, label="align left"
)
with gr.Column():
iop_align_bottom = gr.Number(
value=512, label="align bottom"
)
iop_align_right = gr.Number(
value=512, label="align right"
)
iop_btn = gr.Button("refresh", variant="primary")
with gr.TabItem("Flip"):
flip_axis = gr.Radio(
@ -546,7 +564,14 @@ def add_tab():
tilt_shift_btn.click(tilt_shift.run, input_, image_out)
# iop
all_iop_set = [iop_u, iop_d, iop_l, iop_r]
all_iop_set = [
iop_width,
iop_height,
iop_align_top,
iop_align_bottom,
iop_align_left,
iop_align_right,
]
all_iop_input = [image_other] + all_iop_set
for component in all_iop_set:
_release_if_possible(