Spaces:
Sleeping
Sleeping
import gradio as gr | |
from PIL import Image, ImageFilter | |
import numpy as np | |
import cv2 | |
import matplotlib.pyplot as plt | |
def load_image(image): | |
return image | |
def apply_negative(image): | |
img_np = np.array(image) | |
negative = 255 - img_np | |
return Image.fromarray(negative) | |
def binarize_image(image, threshold): | |
img_np = np.array(image.convert('L')) | |
_, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY) | |
return Image.fromarray(binary) | |
def resize_image(image, width, height): | |
return image.resize((width, height)) | |
def rotate_image(image, angle): | |
return image.rotate(angle) | |
def histo_gray(image): | |
img_np = np.array(image.convert('L')) | |
hist = cv2.calcHist([img_np], [0], None, [256], [0, 256]) | |
plt.plot(hist) | |
plt.title('Histogramme des niveaux de gris') | |
plt.xlabel('Intensité des pixels') | |
plt.ylabel('Nombre de pixels') | |
plt.show() | |
return hist | |
def filtre_gauss(image, kernel_width, kernel_height): | |
img_np = np.array(image) | |
blurred = cv2.GaussianBlur(img_np, (kernel_width, kernel_height), 0) | |
return Image.fromarray(blurred) | |
def erosion(image, taille): | |
img_np = np.array(image.convert('L')) | |
kernel = np.ones((taille, taille), np.uint8) | |
eroded = cv2.erode(img_np, kernel, iterations=1) | |
return Image.fromarray(eroded) | |
def dilatation(image, taille): | |
img_np = np.array(image.convert('L')) | |
kernel = np.ones((taille, taille), np.uint8) | |
dilated = cv2.dilate(img_np, kernel, iterations=1) | |
return Image.fromarray(dilated) | |
def extract_edges(image): | |
img_np = np.array(image.convert('L')) | |
edges = cv2.Canny(img_np, 100, 200) | |
return Image.fromarray(edges) | |
# Interface Gradio | |
def image_processing(image, operation, threshold=128, width=100, height=100, angle=0, kernel_width=5, kernel_height=5, taille_e=3, taille_d=3): | |
if operation == "Négatif": | |
return apply_negative(image) | |
elif operation == "Binarisation": | |
return binarize_image(image, threshold) | |
elif operation == "Redimensionner": | |
return resize_image(image, width, height) | |
elif operation == "Rotation": | |
return rotate_image(image, angle) | |
elif operation == "Histogramme des niveaux de gris": | |
return histo_gray(image) | |
elif operation == "Filtre gaussien": | |
return filtre_gauss(image, kernel_width, kernel_height) | |
elif operation == "Erosion": | |
return erosion(image, taille_e) | |
elif operation == "Dilatation": | |
return dilatation(image, taille_d) | |
elif operation == "Extraction de contours": | |
return extract_edges(image) | |
with gr.Blocks() as demo: | |
gr.Markdown("## Projet de Traitement d'Image") | |
with gr.Row(): | |
image_input = gr.Image(type="pil", label="Charger Image") | |
operation = gr.Radio(["Négatif", "Binarisation", "Redimensionner", "Rotation", "Histogramme des niveaux de gris", "Filtre gaussien", "Extraction de contours", "Erosion", "Dilatation"], label="Opération") | |
threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False) | |
width = gr.Number(value=100, label="Largeur", visible=False) | |
height = gr.Number(value=100, label="Hauteur", visible=False) | |
angle = gr.Number(value=0, label="Angle de Rotation", visible=False) | |
kernel_width = gr.Number(value=5, label="Largeur du kernel du filtre gaussien", visible=False) | |
kernel_height = gr.Number(value=5, label="Hauteur du kernel du filtre gaussien", visible=False) | |
taille_e = gr.Number(value=3, label="Taille du filtre pour l'érosion", visible=False) | |
taille_d = gr.Number(value=3, label="Taille du filtre pour la dilatation", visible=False) | |
image_output = gr.Image(label="Image Modifiée") | |
def update_inputs(operation): | |
if operation == "Binarisation": | |
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) | |
elif operation == "Redimensionner": | |
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) | |
elif operation == "Rotation": | |
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) | |
elif operation == "Filtre gaussien": | |
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False) | |
elif operation == "Erosion": | |
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=False) | |
elif operation == "Dilatation": | |
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True) | |
else: | |
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) | |
operation.change(update_inputs, inputs=operation, outputs=[threshold, width, height, angle, kernel_width, kernel_height, taille_e, taille_d]) | |
submit_button = gr.Button("Appliquer") | |
submit_button.click(image_processing, inputs=[image_input, operation, threshold, width, height, angle, kernel_width, kernel_height, taille_e, taille_d], outputs=image_output) | |
demo.launch(share=True) | |