Avkash commited on
Commit
eb21215
·
1 Parent(s): 8e1a94e

Upload imageprocessui.py

Browse files
Files changed (1) hide show
  1. imageprocessui.py +204 -0
imageprocessui.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+
5
+ image_global = np.zeros([10,10,3], dtype=np.uint8)
6
+
7
+
8
+ def process_source_image(image_source):
9
+ image_out = image_source
10
+ global image_global
11
+ image_global = image_source
12
+ img_info = []
13
+ if image_source is not None:
14
+ if len(image_source.shape) == 3:
15
+ img_info.append({"Height: ": image_source.shape[0]})
16
+ img_info.append({"Width: ": image_source.shape[1]})
17
+ img_info.append({"Channels: ": image_source.shape[2]})
18
+ if len(image_source.shape) == 2:
19
+ img_info.append({"Height: ": image_source.shape[0]})
20
+ img_info.append({"Width: ": image_source.shape[1]})
21
+ return str(img_info), image_out
22
+
23
+
24
+ def image_resizer(img_height, img_width):
25
+ image_out = Image.fromarray(image_global)
26
+ image_out = image_out.resize((img_width, img_height))
27
+ return image_out
28
+
29
+
30
+ def image_convert_grayscale():
31
+ image_out = Image.fromarray(image_global)
32
+ image_out = image_out.convert('L')
33
+ return image_out
34
+
35
+
36
+ def image_process_alpha_channel(alpha_value):
37
+ image_out = Image.fromarray(image_global)
38
+ image_out.putalpha(alpha_value)
39
+ return image_out
40
+
41
+
42
+ def image_process_scratchpad(scratch_img):
43
+ global image_global
44
+ image_global = scratch_img
45
+ image_out = Image.fromarray(scratch_img)
46
+ return image_out
47
+
48
+
49
+ def show_3_channels():
50
+ img_red = img_green = img_blue = image_global
51
+ if len(image_global.shape) == 3 and image_global.shape[2] == 3:
52
+ red_img = np.zeros(image_global.shape)
53
+ red_channel = image_global[:, :, 0]
54
+ red_img[:, :, 0] = red_channel
55
+
56
+ green_img = np.zeros(image_global.shape)
57
+ green_channel = image_global[:, :, 1]
58
+ green_img[:, :, 1] = green_channel
59
+
60
+ blue_img = np.zeros(image_global.shape)
61
+ blue_channel = image_global[:, :, 2]
62
+ blue_img[:, :, 2] = blue_channel
63
+
64
+ img_red = Image.fromarray(red_img.astype(np.uint8))
65
+ img_green = Image.fromarray(green_img.astype(np.uint8))
66
+ img_blue = Image.fromarray(blue_img.astype(np.uint8))
67
+
68
+ return img_red, img_green, img_blue,
69
+
70
+
71
+ class ImageProcessUI(object):
72
+ def __init__(self, ui_obj):
73
+ self.name = "Image Processor UI"
74
+ self.description = "This class is designed to build UI for our application"
75
+ self.ui_obj = ui_obj
76
+
77
+ def create_application_ui(self):
78
+ with self.ui_obj:
79
+ gr.Markdown("Image Processing Application UI with Gradio")
80
+ with gr.Tabs():
81
+ with gr.TabItem("Select your image"):
82
+ with gr.Row():
83
+ with gr.Column():
84
+ img_source = gr.Image(label="Please select source Image")
85
+ source_image_loader = gr.Button("Load above Image")
86
+ with gr.Column():
87
+ output_label = gr.Label(label="Image Info")
88
+ img_output = gr.Image(label="Image Output")
89
+ with gr.TabItem("Resize your Image"):
90
+ with gr.Row():
91
+ with gr.Column():
92
+ resize_label = gr.Label(label="Select your image height and width")
93
+ image_height = gr.Slider(10, 1000, label="Image Height", value=100)
94
+ image_width = gr.Slider(10, 1000, label="Image Width", value=100)
95
+ image_resize_btn = gr.Button("Resize Image")
96
+ with gr.Column():
97
+ img_resize_output = gr.Image(label="Resized Image Output")
98
+ with gr.TabItem("Image as Grayscale"):
99
+ with gr.Row():
100
+ with gr.Column():
101
+ resize_label = gr.Label(label="Source image as GrayScale",
102
+ value="GRAYSCALE IMAGE")
103
+ img_gray_output = gr.Image(label="Grayscale Image Output")
104
+ image_grayscale_btn = gr.Button("Grayscale Your Image")
105
+ with gr.TabItem("Image with Red, Green and Blue Channels"):
106
+ with gr.Row():
107
+ with gr.Column():
108
+ rgb_label = gr.Label(label="Source image with 3 channels",
109
+ value="RED, GREEN and BLUE IMAGE CHANNELS")
110
+ with gr.Row():
111
+ with gr.Column():
112
+ rgb_red_label = gr.Label(label="", value="RED CHANNELS")
113
+ img_red_output = gr.Image(label="Image as Red Channel")
114
+ with gr.Column():
115
+ rgb_green_label = gr.Label(label="", value="GREEN CHANNELS")
116
+ img_green_output = gr.Image(label="Image as Green Channel")
117
+ with gr.Column():
118
+ rgb_blue_label = gr.Label(label="", value="BLUE CHANNELS")
119
+ img_blue_output = gr.Image(label="Image as Blue Channel")
120
+ image_3channels_btn = gr.Button("Show 3 Channels in Image")
121
+ with gr.TabItem("Image with Alpha Channel"):
122
+ with gr.Row():
123
+ with gr.Column():
124
+ alpha_channel_label = gr.Label(label="Source image",
125
+ value="IMAGE WITH ALPHA CHANNEL")
126
+ alpha_channel_value = gr.Slider(1, 256, label="Alpha Channel", value=128)
127
+ img_alpha_channel_output = gr.Image(label="Image with Alpha Channel Output")
128
+ image_alpha_channel_btn = gr.Button("Image with Alpha Channel")
129
+ alpha_channel_value.change(
130
+ image_process_alpha_channel,
131
+ inputs=[alpha_channel_value],
132
+ outputs=[img_alpha_channel_output]
133
+ )
134
+ with gr.TabItem("Image with Scratchpad"):
135
+ with gr.Row():
136
+ with gr.Column():
137
+ img_scratch_pad_src = gr.Sketchpad(label="Draw your image")
138
+ scratch_pd_btn = gr.Button("Update Scribble Image")
139
+ with gr.Column():
140
+ img_scratch_pad_out = gr.Image(label="Scribbled Output")
141
+
142
+ source_image_loader.click(
143
+ process_source_image,
144
+ [
145
+ img_source
146
+ ],
147
+ [
148
+ output_label,
149
+ img_output
150
+ ]
151
+ )
152
+ image_resize_btn.click(
153
+ image_resizer,
154
+ [
155
+ image_height,
156
+ image_width
157
+ ],
158
+ [
159
+ img_resize_output
160
+ ]
161
+ )
162
+ image_grayscale_btn.click(
163
+ image_convert_grayscale,
164
+ [
165
+
166
+ ],
167
+ [
168
+ img_gray_output
169
+ ]
170
+ )
171
+ image_3channels_btn.click(
172
+ show_3_channels,
173
+ [
174
+
175
+ ],
176
+ [
177
+ img_red_output,
178
+ img_green_output,
179
+ img_blue_output
180
+ ]
181
+ )
182
+ image_alpha_channel_btn.click(
183
+ image_process_alpha_channel,
184
+ [
185
+ alpha_channel_value
186
+ ],
187
+ [
188
+ img_alpha_channel_output
189
+ ]
190
+ )
191
+ scratch_pd_btn.click(
192
+ image_process_scratchpad,
193
+ [
194
+ img_scratch_pad_src
195
+ ],
196
+ [
197
+ img_scratch_pad_out
198
+ ]
199
+ )
200
+
201
+ def launch_ui(self):
202
+ self.ui_obj.launch()
203
+
204
+