File size: 4,542 Bytes
0a56124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2982e1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a56124
94c9fb5
 
0a56124
 
1c41fee
0a56124
 
 
4a4842f
0a56124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c41fee
0a56124
 
 
 
 
 
 
 
94c9fb5
4a4842f
0a56124
 
 
 
 
94c9fb5
0a56124
1c41fee
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import spaces
import gradio as gr
import re
from PIL import Image,ImageFilter

import os
import numpy as np




def process_images(fg_image, bg_image,fg_image_mask=None,dilate=0,blur=0):
    # I'm not sure when this happen maybe api calling
    
    #Basically ImageEditor's value are dictionary,If not convert value to dict
    if not isinstance(fg_image, dict):
        if fg_image_mask == None:
             print("empty mask")
             return image,None
        else:
            image = dict({'background': image, 'layers': [fg_image_mask]}) #no need?

    if fg_image_mask!=None:
         mask = fg_image_mask
    else:
         if len(fg_image['layers']) == 0:
              print("empty mask")
              return image,None
         #print("use layer")
         mask = fg_image['layers'][0]

    mask = mask.convert("L")

    if dilate>0:
        if dilate%2 ==0:
             dilate -= 1
        mask = mask.filter(ImageFilter.MaxFilter(dilate))


    if blur>0:
        mask = mask.filter(ImageFilter.GaussianBlur(radius=blur))


    image2 = fg_image["background"].convert("RGBA")
    
    if bg_image == None:
         image2_masked = Image.composite(image2, Image.new("RGBA", image2.size, (0, 0, 0, 0)), mask)
         return image2_masked,mask
    

    bg_image = bg_image.convert("RGBA")
    bg_image.paste(image2, (0, 0), mask)

    return [bg_image,mask]
    

def read_file(path: str) -> str:
    with open(path, 'r', encoding='utf-8') as f:
        content = f.read()

    return content

css="""
#col-left {
    margin: 0 auto;
    max-width: 640px;
}
#col-right {
    margin: 0 auto;
    max-width: 640px;
}
.grid-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap:10px
}

.image {
  width: 128px; 
  height: 128px; 
  object-fit: cover; 
}

.text {
  font-size: 16px;
}
"""

with gr.Blocks(css=css, elem_id="demo-container") as demo:
    with gr.Column():
        gr.HTML(read_file("demo_header.html"))
        gr.HTML(read_file("demo_tools.html"))
    with gr.Row():
                with gr.Column():
                    image = gr.ImageEditor(height=800,sources=['upload','clipboard'],transforms=[],image_mode='RGB', layers=False,  elem_id="Foreground", type="pil", label="Foreground",brush=gr.Brush(colors=["#fff"], color_mode="fixed"))
                    #image.height=1000
                    
                    btn = gr.Button("Paste to BG", elem_id="run_button",variant="primary")
                    
                    bg_image = gr.Image(sources=['upload','clipboard'],  elem_id="bg_image", type="pil", label="Background Image",height=400, value=None)
                    image_mask = gr.Image(sources=['upload','clipboard'],  elem_id="mask_upload", type="pil", label="Mask Uploaded",height=400, value=None)
                    with gr.Accordion(label="Advanced Settings", open=False):
                        with gr.Row( equal_height=True):
                            blur = gr.Slider(
                            label="blur",
                            minimum=0,
                            maximum=100,
                            step=1,
                            value=5)

                            dilate = gr.Slider(
                            label="dilate",
                            minimum=0,
                            maximum=100,
                            step=1,
                            value=0)
                        id_input=gr.Text(label="Name", visible=False)
                            
                with gr.Column():
                    image_out = gr.Image(height=800,sources=[],label="Output", elem_id="output-img",format="webp")
                    mask_out = gr.Image(height=800,sources=[],label="Mask", elem_id="mask-img",format="jpeg")

                    
            

    btn.click(fn=process_images, inputs=[image, bg_image,image_mask,dilate,blur], outputs =[image_out,mask_out], api_name='infer')
    gr.Examples(
               examples=[
                   ["examples/00533245_00004200_eyes.jpg","examples/00533245_00003200_mouth.jpg","examples/00533245_99_mask.jpg",5,18,"examples/00533245_mixed.jpg"],
                   ["examples/00346245_00006200.jpg", "examples/00346245_00003200.jpg","examples/00346245_mask.jpg",10,0,"examples/00346245_mixed.jpg"]
                  
                         ]
                         ,
                inputs=[image,bg_image,image_mask,blur,dilate,image_out]
    )
    gr.HTML(read_file("demo_footer.html"))

    if __name__ == "__main__":
        demo.launch()