File size: 4,198 Bytes
dc14111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import gradio as gr
from gradio.components import Markdown as md
from PIL import Image

demo = gr.Blocks()

io1a = gr.Interface(lambda x: x, gr.Image(), gr.Image())
io1b = gr.Interface(lambda x: x, gr.Image(source="webcam"), gr.Image())

io2a = gr.Interface(lambda x: x, gr.Image(source="canvas"), gr.Image())
io2b = gr.Interface(lambda x: x, gr.Sketchpad(), gr.Image())
io2c = gr.Interface(
    lambda x: x, gr.Image(source="canvas", shape=(512, 512)), gr.Image()
)

io3a = gr.Interface(
    lambda x: [x["mask"], x["image"]],
    gr.Image(source="upload", tool="sketch"),
    [gr.Image(), gr.Image()],
)

io3b = gr.Interface(
    lambda x: [x["mask"], x["image"]],
    gr.ImageMask(),
    [gr.Image(), gr.Image()],
)

io3b2 = gr.Interface(
    lambda x: [x["mask"], x["image"]],
    gr.ImageMask(),
    [gr.Image(), gr.Image()],
)

io3b3 = gr.Interface(
    lambda x: [x["mask"], x["image"]],
    gr.ImageMask(),
    [gr.Image(), gr.Image()],
)

io3c = gr.Interface(
    lambda x: [x["mask"], x["image"]],
    gr.Image(source="webcam", tool="sketch"),
    [gr.Image(), gr.Image()],
)

io4a = gr.Interface(
    lambda x: x, gr.Image(source="canvas", tool="color-sketch"), gr.Image()
)
io4b = gr.Interface(lambda x: x, gr.Paint(), gr.Image())

io5a = gr.Interface(
    lambda x: x, gr.Image(source="upload", tool="color-sketch"), gr.Image()
)
io5b = gr.Interface(lambda x: x, gr.ImagePaint(), gr.Image())
io5c = gr.Interface(
    lambda x: x, gr.Image(source="webcam", tool="color-sketch"), gr.Image()
)


def save_image(image):
    image.save("colorede.png")
    return image


img = Image.new("RGB", (512, 512), (150, 150, 150))
img.save("image.png", "PNG")

io5d = gr.Interface(
    save_image,
    gr.Image("image.png", source="upload", tool="color-sketch", type="pil"),
    gr.Image(),
)

with demo:
    md("# Different Ways to Use the Image Input Component")
    md(
        "**1a. Standalone Image Upload: `gr.Interface(lambda x: x, gr.Image(), gr.Image())`**"
    )
    io1a.render()
    md(
        "**1b. Standalone Image from Webcam: `gr.Interface(lambda x: x, gr.Image(source='webcam'), gr.Image())`**"
    )
    io1b.render()
    md(
        "**2a. Black and White Sketchpad: `gr.Interface(lambda x: x, gr.Image(source='canvas'), gr.Image())`**"
    )
    io2a.render()
    md(
        "**2b. Black and White Sketchpad: `gr.Interface(lambda x: x, gr.Sketchpad(), gr.Image())`**"
    )
    io2b.render()
    md("**2c. Black and White Sketchpad with `shape=(512,512)`**")
    io2c.render()
    md("**3a. Binary Mask with image upload:**")
    md(
        """```python
gr.Interface(
    lambda x: [x['mask'], x['image']],
    gr.Image(source='upload', tool='sketch'),
    [gr.Image(), gr.Image()],
)
```
"""
    )
    io3a.render()
    md("**3b. Binary Mask with image upload:**")
    md(
        """```python
gr.Interface(
    lambda x: [x['mask'], x['image']],
    gr.ImageMask(),
    [gr.Image(), gr.Image()],
)
```
"""
    )
    io3b.render()
    md("**3c. Binary Mask with webcam upload:**")
    md(
        """```python
gr.Interface(
    lambda x: [x['mask'], x['image']],
    gr.Image(source='webcam', tool='sketch'),
    [gr.Image(), gr.Image()],
)
```
"""
    )
    io3c.render()
    md(
        "**4a. Color Sketchpad: `gr.Interface(lambda x: x, gr.Image(source='canvas', tool='color-sketch'), gr.Image())`**"
    )
    io4a.render()
    md("**4b. Color Sketchpad: `gr.Interface(lambda x: x, gr.Paint(), gr.Image())`**")
    io4b.render()
    md(
        "**5a. Color Sketchpad with image upload: `gr.Interface(lambda x: x, gr.Image(source='upload', tool='color-sketch'), gr.Image())`**"
    )
    io5a.render()
    md(
        "**5b. Color Sketchpad with image upload: `gr.Interface(lambda x: x, gr.ImagePaint(), gr.Image())`**"
    )
    io5b.render()
    md(
        "**5c. Color Sketchpad with webcam upload: `gr.Interface(lambda x: x, gr.Image(source='webcam', tool='color-sketch'), gr.Image())`**"
    )
    io5c.render()
    md("**Tabs**")
    with gr.Tab("One"):
        io3b2.render()
    with gr.Tab("Two"):
        io3b3.render()
    md("**5d. Color Sketchpad with image upload and a default images**")
    io5d.render()

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