Add InOutPaint

pull/21/head
Kohaku-Blueleaf 2023-01-21 20:57:31 +08:00
parent 73f1a1da66
commit 31122529ed
9 changed files with 103 additions and 2 deletions

0
inoutpaint/__init__.py Normal file
View File

56
inoutpaint/main.py Normal file
View File

@ -0,0 +1,56 @@
import numpy as np
import cv2
from PIL import Image
try:
from .utils import *
except:
from utils import *
HTML_TEMPLATES = {
'resolution': '''<textarea>'''
}
def run(img, u, d, l, r, mode='fill'):
img = Image.fromarray(img)
new_img, mask = resize_with_mask(
img,
(u, d, 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 l:
new_img.paste(new_img.resize((l, h_n), box=(l, 0, l, h_n)), box=(0, 0))
if r:
new_img.paste(new_img.resize((r, h_n), box=(w+l, 0, w+l, h_n)), box=(l+w, 0))
return new_img, mask, f'{w_n} x {h_n}'
if __name__ == '__main__':
img = Image.open('./01491-2023-01-21_d7ff2a1d60_KBlueLeaf_KBlueLeaf_856221927-768x512.png')
new, mask = run(np.array(img), 64, 192, 64, 64)
w, h = new.size
if w>h:
demo = Image.new('RGB', (w, h*2))
demo.paste(new, box=(0, 0))
demo.paste(mask, box=(0, h))
else:
demo = Image.new('RGB', (w*2, h))
demo.paste(new, box=(0, 0))
demo.paste(mask, box=(w, 0))
new.save('./test-img.png')
mask.save('./test-mask.png')
demo.show()

19
inoutpaint/utils.py Normal file
View File

@ -0,0 +1,19 @@
import numpy as np
import cv2
from PIL import Image
def resize_with_mask(
img: Image.Image,
expands: tuple[int, int, int, int]
) -> tuple[Image.Image, Image.Image]:
w, h = img.size
u, d, l, r = expands
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))
return new_img, mask

View File

@ -16,6 +16,7 @@ from hakuimg import(
sketch,
pixel
)
from inoutpaint import main as outpaint
'''
@ -132,17 +133,26 @@ def add_tab():
pixel_btn = gr.Button("refresh", variant="primary")
with gr.TabItem('Other'):
img_other_h_slider = gr.Slider(160, 1280, 320, step=10, label="Image preview height", elem_id='haku_img_h_oth')
image_other = gr.Image(type='numpy', label="img", elem_id='haku_img_other', show_label=False)
with gr.Tabs(elem_id='function list'):
with gr.TabItem('InOutPaint'):
iop_mask = gr.Image(type='pil', visible=False)
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')
iop_btn = gr.Button("refresh", variant="primary")
with gr.Column():
img_out_h_slider = gr.Slider(160, 1280, 420, step=10, label="Image preview height", elem_id='haku_img_h_out')
res_info = gr.Textbox(label='Resolution')
image_out = gr.Image(
interactive=False,
type='pil',
label="haku_output",
elem_id='haku_out'
)
image_mask = gr.Image(visible=False)
with gr.Row():
send_btns = gpc.create_buttons(["img2img", "inpaint", "extras"])
send_ip_b = gr.Button("Send to inpaint upload", elem_id='send_inpaint_base')
@ -152,8 +162,12 @@ def add_tab():
for i in range(1, layers+1):
send_blends.append(gr.Button(f"Send to Layer{i}", elem_id=f'send_haku_blend{i}'))
send_eff = gr.Button("Send to Effect", elem_id='send_haku_blur')
#preview height slider
img_blend_h_slider.change(None, img_blend_h_slider, _js=f'get_change_height("haku_img_blend")')
img_eff_h_slider.change(None, img_eff_h_slider, _js=f'get_change_height("haku_img_eff")')
img_other_h_slider.change(None, img_other_h_slider, _js=f'get_change_height("haku_img_other")')
img_out_h_slider.change(None, img_out_h_slider, _js=f'get_change_height("haku_out")')
# blend
all_blend_set = [bg_color]
@ -197,6 +211,18 @@ def add_tab():
component.change(pixel.run, all_p_input, image_out)
pixel_btn.click(pixel.run, all_p_input, image_out)
#iop
all_iop_set = [
iop_u, iop_d, iop_l, iop_r
]
all_iop_input = [image_other] + all_iop_set
for component in all_iop_set:
component.change(outpaint.run, all_iop_input, [image_out, image_mask])
iop_btn.click(outpaint.run, all_iop_input, [image_out, image_mask])
#
image_out.change(lambda x:f'{x.width} x {x.height}', image_out, res_info)
#send
for btns, btn3, img_src in all_btns:
for btn, img in zip(btns, all_layers):
@ -212,7 +238,7 @@ def add_tab():
send_btn.click(lambda x:x, image_out, layer)
send_btn.click(None, _js='switch_to_haku_blend')
send_ip_b.click(lambda *x:x, [image_out, iop_mask], [inpaint_base, inpaint_mask])
send_ip_b.click(lambda *x:x, [image_out, image_mask], [inpaint_base, inpaint_mask])
send_ip_b.click(None, _js = 'switch_to_inpaint_upload')
send_eff.click(lambda x:x, image_out, image_eff)