File size: 1,760 Bytes
ccef207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gradio as gr
from PIL import Image
import numpy as np
import cv2

# Fonction pour charger et afficher l'image
def load_image(image):
    return image

# Fonction pour appliquer un négatif
def apply_negative(image):
    img_np = np.array(image)
    negative = 255 - img_np
    return Image.fromarray(negative)

# Fonction pour binariser une image
def binarize_image(image, threshold):
    img_np = np.array(image.convert('L'))  # Convertir en niveaux de gris
    _, binary = cv2.threshold(img_np, threshold, 255, cv2.THRESH_BINARY)
    return Image.fromarray(binary)

# Interface Gradio
def image_processing(image, operation, threshold=128):
    if operation == "Négatif":
        return apply_negative(image)
    elif operation == "Binarisation":
        return binarize_image(image, threshold)
    return image

# Interface Gradio
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"], label="Opération à effectuer", value="Négatif")
        threshold = gr.Slider(0, 255, 128, label="Seuil de binarisation", visible=False)
    
    image_output = gr.Image(label="Image Modifiée")

    def update_threshold(operation):
        if operation == "Binarisation":
            return gr.update(visible=True)
        return gr.update(visible=False)
    
    operation.change(update_threshold, inputs=operation, outputs=threshold)
    
    submit_button = gr.Button("Appliquer")
    submit_button.click(image_processing, inputs=[image_input, operation, threshold], outputs=image_output)

# Lancer l'application Gradio
demo.launch()