Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageFilter
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
from skimage import morphology
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import io
|
8 |
+
|
9 |
+
# Images manipulation functions
|
10 |
+
|
11 |
+
|
12 |
+
#Image loading
|
13 |
+
def load_image(image_path):
|
14 |
+
'''Load image with PIL'''
|
15 |
+
image=Image.open(image_path)
|
16 |
+
return image
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
#Negative APPLYING
|
21 |
+
def apply_negative(image):
|
22 |
+
'''
|
23 |
+
input: PIL Image
|
24 |
+
|
25 |
+
output : PIL Image
|
26 |
+
|
27 |
+
Image loaded with PIL is turned to numpy format. Then, we calculate the new pixels values and image gotten is return to PIL format'''
|
28 |
+
|
29 |
+
img_np = np.array(image)
|
30 |
+
negative = 255 - img_np
|
31 |
+
return Image.fromarray(negative)
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
#binarization
|
36 |
+
def binarize_image(image, threshold_value):
|
37 |
+
'''
|
38 |
+
inputs : PIL image ; threshold_value
|
39 |
+
|
40 |
+
output : PIL image
|
41 |
+
|
42 |
+
Image in PIL format is converted into grayscale format and then into numpy format.Now we make a binary threshold base on threshold value.
|
43 |
+
Image gotten is returned to Image format'''
|
44 |
+
|
45 |
+
|
46 |
+
img_np = np.array(image.convert('L'))
|
47 |
+
_, binary = cv2.threshold(img_np, threshold_value, 255, cv2.THRESH_BINARY)
|
48 |
+
return Image.fromarray(binary)
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
#image resizing
|
55 |
+
def resize_image(image, width, height):
|
56 |
+
'''Resizing is doing by using PIL resizing method'''
|
57 |
+
|
58 |
+
return image.resize((width, height))
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
#image rotation
|
64 |
+
def rotate_image(image, angle):
|
65 |
+
'''Rotation is doing by using PIL rotation method'''
|
66 |
+
|
67 |
+
return image.rotate(angle)
|
68 |
+
|
69 |
+
|
70 |
+
|
71 |
+
#Image histogram
|
72 |
+
def histogram(image):
|
73 |
+
|
74 |
+
img = np.array(image.convert('L'))
|
75 |
+
hist = cv2.calcHist([img],[0],None,[256],[0,256])
|
76 |
+
plt.plot(hist)
|
77 |
+
|
78 |
+
img_buf = io.BytesIO()
|
79 |
+
plt.savefig(img_buf, format='png')
|
80 |
+
|
81 |
+
return Image.open(img_buf)
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
#Gaussian filter
|
88 |
+
def g_filter(image):
|
89 |
+
img_gauss = image.filter(ImageFilter.GaussianBlur(5) )
|
90 |
+
|
91 |
+
return img_gauss
|
92 |
+
|
93 |
+
|
94 |
+
#Sobel
|
95 |
+
def sobel_f(image):
|
96 |
+
i = np.array(image)
|
97 |
+
img = cv2.GaussianBlur(i, (3, 3), sigmaX=0, sigmaY=0)
|
98 |
+
|
99 |
+
edge_sobel = cv2.Sobel(src=img, ddepth=cv2.CV_8U, dx=1, dy=1, ksize=5)
|
100 |
+
return Image.fromarray(edge_sobel)
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
#erosion
|
105 |
+
def erosion(image):
|
106 |
+
i=np.array(image.convert('L'))
|
107 |
+
ero_img= morphology.binary_erosion(i, morphology.disk(1))
|
108 |
+
return Image.fromarray(ero_img)
|
109 |
+
|
110 |
+
|
111 |
+
#dilatation
|
112 |
+
def dilatation(image):
|
113 |
+
i=np.array(image.convert('L'))
|
114 |
+
ero_img= morphology.binary_dilation(i, morphology.disk(1))
|
115 |
+
return Image.fromarray(ero_img)
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
#contour
|
120 |
+
def contour(image):
|
121 |
+
return image.filter(ImageFilter.CONTOUR)
|
122 |
+
|
123 |
+
|
124 |
+
#lumineux
|
125 |
+
def lumineux(image):
|
126 |
+
return image.filter(ImageFilter.EDGE_ENHANCE)
|
127 |
+
|
128 |
+
#Netteté
|
129 |
+
def nette(image):
|
130 |
+
return image.filter(ImageFilter.SHARPEN)
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
# Interface Gradio
|
138 |
+
def image_processing(image, operation, threshold=128, width=100, height=100, angle=0):
|
139 |
+
if operation == "Négatif":
|
140 |
+
return apply_negative(image)
|
141 |
+
elif operation == "Binarisation":
|
142 |
+
return binarize_image(image, threshold)
|
143 |
+
elif operation == "Redimensionner":
|
144 |
+
return resize_image(image, width, height)
|
145 |
+
elif operation == "Rotation":
|
146 |
+
return rotate_image(image, angle)
|
147 |
+
elif operation == "Histogramme":
|
148 |
+
return histogram(image)
|
149 |
+
elif operation == "Gaussian Filter":
|
150 |
+
return g_filter(image)
|
151 |
+
elif operation == "Sobel":
|
152 |
+
return sobel_f(image)
|
153 |
+
elif operation == "Erosion":
|
154 |
+
return erosion(image)
|
155 |
+
elif operation == "Dilatation":
|
156 |
+
return erosion(image)
|
157 |
+
elif operation == "Contour":
|
158 |
+
return contour(image)
|
159 |
+
elif operation == "Luminosité":
|
160 |
+
return lumineux(image)
|
161 |
+
elif operation == "Netteté":
|
162 |
+
return nette(image)
|
163 |
+
return image
|
164 |
+
|
165 |
+
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
+
|
173 |
+
# Gradio Interface
|
174 |
+
with gr.Blocks() as demo:
|
175 |
+
gr.Markdown("## Mini photoshop")
|
176 |
+
|
177 |
+
with gr.Row():
|
178 |
+
image_input = gr.Image(type="pil", label="Charger Image")
|
179 |
+
operation = gr.Radio(["Négatif", "Binarisation", "Redimensionner", "Rotation","Histogramme","Gaussian Filter","Sobel", "Erosion","Dilatation","Luminosité","Contour", "Netteté"], label="Opération")
|
180 |
+
|
181 |
+
|
182 |
+
threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=True)
|
183 |
+
width = gr.Number(value=100, label="Largeur de redimensionnement", visible=True)
|
184 |
+
height = gr.Number(value=100, label="Hauteur de redimensionnement", visible=True)
|
185 |
+
angle = gr.Number(value=0, label="Angle de Rotation", visible=True)
|
186 |
+
|
187 |
+
|
188 |
+
|
189 |
+
image_output = gr.Image(label="Image Modifiée")
|
190 |
+
|
191 |
+
submit_button = gr.Button("Appliquer")
|
192 |
+
submit_button.click(image_processing, inputs=[image_input, operation, threshold, width, height, angle], outputs=image_output)
|
193 |
+
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
+
# Launch application
|
198 |
+
demo.launch()
|