Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from PIL import Image | |
| import numpy as np | |
| import cv2 | |
| import matplotlib.pyplot as plt | |
| import sys | |
| from PIL import Image,ImageFilter | |
| # Fonctions de traitement d'image | |
| 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): | |
| #image=image.convert('L') | |
| return image.resize((width, height)) | |
| def rotate_image(image, angle): | |
| return image.rotate(angle) | |
| def hister(image): | |
| #image=cv2.imread(image,cv2.IMREAD_GRAYSCALE) | |
| image=np.array(image.convert('L')) | |
| if image is None: | |
| print('Image load failed !') | |
| sys.exit() | |
| hist=cv2.calcHist([image],[0],None,[256],[0,256]) | |
| #cv2.imshow('Gray Scale Histogram',image) | |
| #cv2.waitkey(1) | |
| #plt.plot(hist) | |
| fig, ax = plt.subplots() | |
| ax.plot(hist) | |
| fig.canvas.draw() | |
| img_plot = np.array(fig.canvas.renderer.buffer_rgba()) | |
| #cv2.imshow('Image', cv2.cvtColor(img_plot, cv2.COLOR_RGBA2BGR)) | |
| #cv2.waitKey(0) | |
| #image_result=plt.savefig('histogram.png') | |
| #plt.show() | |
| #image_result=Image.open('histogram.png') | |
| #image_result=cv2.imread('histogram.png') | |
| return Image.fromarray(img_plot) | |
| def gauss_filterer(image,radius): | |
| return image.filter(ImageFilter.GaussianBlur(radius)) | |
| def contour_extraction(image): | |
| image=image.convert('L') | |
| return image.filter(ImageFilter.FIND_EDGES) | |
| def erode(image): | |
| return image.filter(ImageFilter.MinFilter(3)) | |
| def dilate(image): | |
| return image.filter(ImageFilter.MaxFilter(3)) | |
| # Ajoutez d'autres fonctions pour l'histogramme, le filtrage, Sobel, etc. | |
| # Interface Gradio | |
| def image_processing(image, operation, threshold=128, width=100, height=100, angle=45,radius=9): | |
| 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": | |
| #print("Yes ....") | |
| return hister(image) | |
| elif operation=="Filtre": | |
| return gauss_filterer(image,radius) | |
| elif operation=="Contour": | |
| return contour_extraction(image) | |
| elif operation=="Erosion": | |
| return erode(image) | |
| elif operation=="Dilatation": | |
| return dilate(image) | |
| # Ajouter d'autres conditions pour les autres opérations | |
| return image | |
| # Interface Gradio | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## Image Processing") | |
| gr.Markdown("This is the result of my Week 2 work based on image processing and filters .Let me make you discover it!") | |
| with gr.Row(): | |
| image_input = gr.Image(type="pil", label="Charger Image") | |
| operation = gr.Radio(["Négatif", "Binarisation", "Redimensionner", "Rotation","Histogramme","Filtre","Contour","Erosion","Dilatation"], label="Opération") | |
| #advanced_operation=gr.Radio(["Histogramme"],label="Advanced") | |
| 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) | |
| image_output = gr.Image(label="Image Modifiée") | |
| submit_button = gr.Button("Appliquer") | |
| submit_button.click(fn=image_processing, inputs=[image_input, operation, threshold, width, height, angle], outputs=image_output) | |
| # Lancer l'application Gradio | |
| demo.launch() |