Bonjour,
Je débute aujourd'hui en python et je cherche à créer un menu qui puisse sélectionner une image et faire du traitement d'image suivant le menu que l'on aura sélectionner comma par exemple transformer cette image en niveau de gris.
Cependant, je sollicite votre aide car j'arrive à ouvrir l'explorateur afin de sélectionner l'image mais je n'arrive pas à réutiliser cette dernière afin de pouvoir faire des opération dessus.
Merci de votre précieuse aide !
Et voici mon code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
class MyWindow(Tk):
 
    def __init__(self):
        Tk.__init__(self)
        self.createMenuBar()
 
        self.geometry( "300x200" )
        self.title( "Mon menu" )
 
    def createMenuBar(self):
        menuBar = Menu(self)
 
        menuFile = Menu(menuBar, tearoff=0)
        menuFile.add_command(label="New", command=self.doSomething)
        menuFile.add_command(label="Open", command=self.openFile)
        menuFile.add_command(label="Save", command=self.doSomething)
        menuFile.add_separator()
        menuFile.add_command(label="Conversion en noir", command=self.doSomething1)
        menuFile.add_separator()
        menuFile.add_command(label="Exit", command=self.quit)
        menuBar.add_cascade( label="File", menu=menuFile)
 
        menuEdit = Menu(menuBar, tearoff=0)
        menuEdit.add_command(label="Undo", command=self.doSomething)
        menuEdit.add_separator()
        menuEdit.add_command(label="Copy", command=self.doSomething)
        menuEdit.add_command(label="Cut", command=self.doSomething)
        menuEdit.add_command(label="Paste", command=self.doSomething)
        menuBar.add_cascade( label="Edit", menu=menuEdit)
 
 
 
        menuHelp = Menu(menuBar, tearoff=0)
        menuHelp.add_command(label="About", command=self.doAbout)
        menuBar.add_cascade( label="Help", menu=menuHelp)
 
 
 
        self.config(menu = menuBar)        
 
    def openFile(self):
        file = askopenfilename(title="Choose the file to open", 
                filetypes=[("PNG image", ".png"), ("GIF image", ".gif"), ("All files", ".*")])
        print( file )
 
 
    def doSomething(self):
        print("Menu clicked")
 
    def doSomething1(self):
        print("Menu clicked wesh")
        Mafenetre = Tk()                
        Mafenetre.title("Image")                                  # Titre de la fenetre
        Canevas = Canvas(Mafenetre)              
 
        filename = tkinter.filedialog.askopenfilename(title="Ouvrir une image",filetypes=[('jpg files','.jpg'),('bmp files','.bmp'),('all files','.*')]) 
 
        photo = ImageTk.PhotoImage(file=filename) 
 
        def rgb2gray(rgb):
            return np.dot(rgb[...,:3], [0.299, 0.587, 0.144])
 
        img = mpimg.imread(photo)
        gray = rgb2gray(img)
        plt.imshow(gray, cmap = plt.get_cmap('gray'))
        plt.show()
 
        print (root.filename)
 
 
    def doAbout(self):
        messagebox.showinfo("My title", "My message")
 
 
window = MyWindow()