Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image, ImageOps
|
5 |
+
|
6 |
+
def extract_components(image, component):
|
7 |
+
image = np.array(image)
|
8 |
+
|
9 |
+
if component == "Red Channel":
|
10 |
+
red_channel = image.copy()
|
11 |
+
red_channel[:, :, 1] = 0 # Zero out the green channel
|
12 |
+
red_channel[:, :, 2] = 0 # Zero out the blue channel
|
13 |
+
return Image.fromarray(red_channel)
|
14 |
+
|
15 |
+
elif component == "Green Channel":
|
16 |
+
green_channel = image.copy()
|
17 |
+
green_channel[:, :, 0] = 0 # Zero out the red channel
|
18 |
+
green_channel[:, :, 2] = 0 # Zero out the blue channel
|
19 |
+
return Image.fromarray(green_channel)
|
20 |
+
|
21 |
+
elif component == "Blue Channel":
|
22 |
+
blue_channel = image.copy()
|
23 |
+
blue_channel[:, :, 0] = 0 # Zero out the red channel
|
24 |
+
blue_channel[:, :, 1] = 0 # Zero out the green channel
|
25 |
+
return Image.fromarray(blue_channel)
|
26 |
+
|
27 |
+
elif component == "Grayscale":
|
28 |
+
grayscale = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
29 |
+
return Image.fromarray(grayscale)
|
30 |
+
|
31 |
+
elif component == "Edges":
|
32 |
+
grayscale = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
33 |
+
edges = cv2.Canny(grayscale, 100, 200)
|
34 |
+
return Image.fromarray(edges)
|
35 |
+
|
36 |
+
elif component == "Original":
|
37 |
+
return Image.fromarray(image)
|
38 |
+
|
39 |
+
else:
|
40 |
+
return Image.fromarray(image)
|
41 |
+
|
42 |
+
# Define the Gradio interface
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=extract_components,
|
45 |
+
inputs=[
|
46 |
+
gr.Image(label="Input Image"),
|
47 |
+
gr.Radio(["Red Channel", "Green Channel", "Blue Channel", "Grayscale", "Edges", "Original"], label="Select Component")
|
48 |
+
],
|
49 |
+
outputs=gr.Image(label="Output Image"),
|
50 |
+
title="Image to Components",
|
51 |
+
description="Extract different components from an image (e.g., RGB channels, grayscale, edges)",
|
52 |
+
theme=Nymbo/Nymbo_theme_5
|
53 |
+
)
|
54 |
+
|
55 |
+
# Launch the app
|
56 |
+
iface.launch()
|