| import numpy as np |
| import matplotlib.pyplot as plt |
| import gradio as gr |
| from skimage import exposure, transform |
| from scipy import signal |
| from skimage.io import imread |
| from io import BytesIO |
| import cv2 |
| from PIL import Image |
|
|
| |
| def importer_image(image): |
| return image |
|
|
| |
| def appliquer_negatif(image): |
| image_pix = np.array(image) |
| negative = 255 - image_pix |
| return Image.fromarray(negative) |
|
|
| |
| def binarizer(image, threshold): |
| img_np = np.array(image.convert('L')) |
| _, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY) |
| return Image.fromarray(binary) |
|
|
| |
| def redimensionner(image, largeur, hauteur): |
| largeur, hauteur = int(largeur), int(hauteur) |
| resized_img = image.resize((largeur, hauteur)) |
| return resized_img |
|
|
| |
| def rotation(image, angle): |
| rotated_image = image.convert('RGB').rotate(angle, expand=True) |
| return rotated_image |
|
|
| |
| def histo(image): |
| grayscale = image.convert('L') |
| histogram = grayscale.histogram() |
| |
| plt.figure(figsize=(6, 4)) |
| plt.plot(histogram, color='black') |
| plt.title("Histogramme des Niveaux de Gris") |
| plt.xlabel("Niveaux de Gris (0-255)") |
| plt.ylabel("Nombre de Pixels") |
| |
| |
| buffer = BytesIO() |
| plt.savefig(buffer, format="png") |
| buffer.seek(0) |
| plt.close() |
| |
| return Image.open(buffer) |
|
|
| |
| def gaussien(image, filter_type, kernel_size): |
| opencv_image = np.array(image.convert('RGB')) |
| |
| if filter_type == "Filtre Moyen": |
| filtered_image = cv2.blur(opencv_image, (kernel_size, kernel_size)) |
| else: |
| filtered_image = cv2.GaussianBlur(opencv_image, (kernel_size, kernel_size), 0) |
| |
| return Image.fromarray(filtered_image) |
|
|
| |
| def sobel(image): |
| opencv_image = np.array(image.convert('L')) |
| sobel_x = cv2.Sobel(opencv_image, cv2.CV_64F, 1, 0, ksize=5) |
| sobel_y = cv2.Sobel(opencv_image, cv2.CV_64F, 0, 1, ksize=5) |
| sobel_combined = cv2.magnitude(sobel_x, sobel_y) |
| sobel_combined = np.uint8(np.absolute(sobel_combined)) |
| return Image.fromarray(sobel_combined) |
|
|
| |
| def morphologie(image, operation, kernel_size): |
| opencv_image = np.array(image.convert('L')) |
| kernel = np.ones((kernel_size, kernel_size), np.uint8) |
| |
| if operation == "Érosion": |
| morphed_image = cv2.erode(opencv_image, kernel, iterations=1) |
| else: |
| morphed_image = cv2.dilate(opencv_image, kernel, iterations=1) |
| |
| return Image.fromarray(morphed_image) |
|
|
| |
| def grayscale(image): |
| grayscale_image = image.convert('L') |
| return grayscale_image |
|
|
|
|
|
|
| with gr.Blocks() as demo: |
| with gr.Row(): |
| |
| with gr.Column(): |
| image_input = gr.Image(label="Charger une image", type="pil") |
| neg_button = gr.Button("Appliquer Négatif") |
| bin_slider = gr.Slider(0, 255, value=128, step=1, label="Seuil de Binarisation") |
| bin_button = gr.Button("Binariser l'image") |
| width_slider = gr.Slider(1, 1024, value=256, step=1, label="Largeur (Redimensionner)") |
| height_slider = gr.Slider(1, 1024, value=256, step=1, label="Hauteur (Redimensionner)") |
| resize_button = gr.Button("Redimensionner") |
| rotate_slider = gr.Slider(0, 360, value=0, step=1, label="Angle de Rotation") |
| rotate_button = gr.Button("Effectuer une rotation") |
| grayscale_button = gr.Button("Afficher en Niveaux de Gris") |
| filter_type = gr.Radio(["Filtre Moyen", "Filtre Gaussien"], label="Type de Filtre") |
| kernel_slider = gr.Slider(1, 15, value=3, step=2, label="Taille du Kernel") |
| filter_button = gr.Button("Appliquer Filtre") |
| sobel_button = gr.Button("Appliquer Sobel") |
| morph_op = gr.Radio(["Érosion", "Dilatation"], label="Opération Morphologique") |
| morph_button = gr.Button("Appliquer Morphologie") |
| |
|
|
| |
| with gr.Column(): |
| image_output = gr.Image(label="Image Modifiée") |
| histogram_output = gr.Image(label="Histogramme des Niveaux de Gris") |
| download_link = gr.HTML() |
|
|
| |
| neg_button.click(appliquer_negatif, inputs=image_input, outputs=image_output) |
| bin_button.click(binarizer, inputs=[image_input, bin_slider], outputs=image_output) |
| resize_button.click(redimensionner, inputs=[image_input, width_slider, height_slider], outputs=image_output) |
| rotate_button.click(rotation, inputs=[image_input, rotate_slider], outputs=image_output) |
| grayscale_button.click(grayscale, inputs=image_input, outputs=image_output) |
| filter_button.click(gaussien, inputs=[image_input, filter_type, kernel_slider], outputs=image_output) |
| sobel_button.click(sobel, inputs=image_input, outputs=image_output) |
| morph_button.click(morphologie, inputs=[image_input, morph_op, kernel_slider], outputs=image_output) |
| |
| |
| image_input.change(histo, inputs=image_input, outputs=histogram_output) |
|
|
|
|
| demo.launch() |
|
|