File size: 3,843 Bytes
2295cd4
 
 
6870a99
2295cd4
6870a99
2295cd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6870a99
2295cd4
6870a99
 
2295cd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6870a99
2295cd4
 
 
 
6870a99
2295cd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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()