File size: 8,044 Bytes
eb21215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import gradio as gr
import numpy as np
from PIL import Image

image_global = np.zeros([10,10,3], dtype=np.uint8)


def process_source_image(image_source):
    image_out = image_source
    global image_global
    image_global = image_source
    img_info = []
    if image_source is not None:
        if len(image_source.shape) == 3:
            img_info.append({"Height: ": image_source.shape[0]})
            img_info.append({"Width: ": image_source.shape[1]})
            img_info.append({"Channels: ": image_source.shape[2]})
        if len(image_source.shape) == 2:
            img_info.append({"Height: ": image_source.shape[0]})
            img_info.append({"Width: ": image_source.shape[1]})
    return str(img_info), image_out


def image_resizer(img_height, img_width):
    image_out = Image.fromarray(image_global)
    image_out = image_out.resize((img_width, img_height))
    return image_out


def image_convert_grayscale():
    image_out = Image.fromarray(image_global)
    image_out = image_out.convert('L')
    return image_out


def image_process_alpha_channel(alpha_value):
    image_out = Image.fromarray(image_global)
    image_out.putalpha(alpha_value)
    return image_out


def image_process_scratchpad(scratch_img):
    global image_global
    image_global = scratch_img
    image_out = Image.fromarray(scratch_img)
    return image_out


def show_3_channels():
    img_red = img_green = img_blue = image_global
    if len(image_global.shape) == 3 and image_global.shape[2] == 3:
        red_img = np.zeros(image_global.shape)
        red_channel = image_global[:, :, 0]
        red_img[:, :, 0] = red_channel

        green_img = np.zeros(image_global.shape)
        green_channel = image_global[:, :, 1]
        green_img[:, :, 1] = green_channel

        blue_img = np.zeros(image_global.shape)
        blue_channel = image_global[:, :, 2]
        blue_img[:, :, 2] = blue_channel

        img_red = Image.fromarray(red_img.astype(np.uint8))
        img_green = Image.fromarray(green_img.astype(np.uint8))
        img_blue = Image.fromarray(blue_img.astype(np.uint8))

    return img_red, img_green, img_blue,


class ImageProcessUI(object):
    def __init__(self, ui_obj):
        self.name = "Image Processor UI"
        self.description = "This class is designed to build UI for our application"
        self.ui_obj = ui_obj

    def create_application_ui(self):
        with self.ui_obj:
            gr.Markdown("Image Processing Application UI with Gradio")
            with gr.Tabs():
                with gr.TabItem("Select your image"):
                    with gr.Row():
                        with gr.Column():
                            img_source = gr.Image(label="Please select source Image")
                            source_image_loader = gr.Button("Load above Image")
                        with gr.Column():
                            output_label = gr.Label(label="Image Info")
                            img_output = gr.Image(label="Image Output")
                with gr.TabItem("Resize your Image"):
                    with gr.Row():
                        with gr.Column():
                            resize_label = gr.Label(label="Select your image height and width")
                            image_height = gr.Slider(10, 1000, label="Image Height", value=100)
                            image_width = gr.Slider(10, 1000, label="Image Width", value=100)
                            image_resize_btn = gr.Button("Resize Image")
                        with gr.Column():
                            img_resize_output = gr.Image(label="Resized Image Output")
                with gr.TabItem("Image as Grayscale"):
                    with gr.Row():
                        with gr.Column():
                            resize_label = gr.Label(label="Source image as GrayScale",
                                                    value="GRAYSCALE IMAGE")
                            img_gray_output = gr.Image(label="Grayscale Image Output")
                            image_grayscale_btn = gr.Button("Grayscale Your Image")
                with gr.TabItem("Image with Red, Green and Blue Channels"):
                    with gr.Row():
                        with gr.Column():
                            rgb_label = gr.Label(label="Source image with 3 channels",
                                                    value="RED, GREEN and BLUE IMAGE CHANNELS")
                    with gr.Row():
                        with gr.Column():
                            rgb_red_label = gr.Label(label="", value="RED CHANNELS")
                            img_red_output = gr.Image(label="Image as Red Channel")
                        with gr.Column():
                            rgb_green_label = gr.Label(label="", value="GREEN CHANNELS")
                            img_green_output = gr.Image(label="Image as Green Channel")
                        with gr.Column():
                            rgb_blue_label = gr.Label(label="", value="BLUE CHANNELS")
                            img_blue_output = gr.Image(label="Image as Blue Channel")
                    image_3channels_btn = gr.Button("Show 3 Channels in Image")
                with gr.TabItem("Image with Alpha Channel"):
                    with gr.Row():
                        with gr.Column():
                            alpha_channel_label = gr.Label(label="Source image",
                                                    value="IMAGE WITH ALPHA CHANNEL")
                            alpha_channel_value = gr.Slider(1, 256, label="Alpha Channel", value=128)
                            img_alpha_channel_output = gr.Image(label="Image with Alpha Channel Output")
                            image_alpha_channel_btn = gr.Button("Image with Alpha Channel")
                            alpha_channel_value.change(
                                image_process_alpha_channel,
                                inputs=[alpha_channel_value],
                                outputs=[img_alpha_channel_output]
                            )
                with gr.TabItem("Image with Scratchpad"):
                    with gr.Row():
                        with gr.Column():
                            img_scratch_pad_src = gr.Sketchpad(label="Draw your image")
                            scratch_pd_btn = gr.Button("Update Scribble Image")
                        with gr.Column():
                            img_scratch_pad_out = gr.Image(label="Scribbled Output")

            source_image_loader.click(
                process_source_image,
                [
                    img_source
                ],
                [
                    output_label,
                    img_output
                ]
            )
            image_resize_btn.click(
                image_resizer,
                [
                    image_height,
                    image_width
                ],
                [
                    img_resize_output
                ]
            )
            image_grayscale_btn.click(
                image_convert_grayscale,
                [

                ],
                [
                    img_gray_output
                ]
            )
            image_3channels_btn.click(
                show_3_channels,
                [

                ],
                [
                    img_red_output,
                    img_green_output,
                    img_blue_output
                ]
            )
            image_alpha_channel_btn.click(
                image_process_alpha_channel,
                [
                    alpha_channel_value
                ],
                [
                    img_alpha_channel_output
                ]
            )
            scratch_pd_btn.click(
                image_process_scratchpad,
                [
                    img_scratch_pad_src
                ],
                [
                    img_scratch_pad_out
                ]
            )

    def launch_ui(self):
        self.ui_obj.launch()