test.py 10.1 KB
Newer Older
Killian Henry's avatar
Killian Henry committed
1 2 3 4
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 24 21:48:54 2017

Killian Henry's avatar
Killian Henry committed
5
@author: Abdessamad et Killian
Killian Henry's avatar
Killian Henry committed
6
"""
Killian Henry's avatar
Killian Henry committed
7
from PIL import Image 
Killian Henry's avatar
Killian Henry committed
8 9 10 11
import os
import tkinter as tk

chemin_image = None
Killian Henry's avatar
Killian Henry committed
12
imagemodèle = None
Killian Henry's avatar
Killian Henry committed
13 14 15
image = None
quitter = 0
revenir_menu_principal = 0
Killian Henry's avatar
Killian Henry committed
16
image2 = None
Killian Henry's avatar
Killian Henry committed
17

Killian Henry's avatar
Killian Henry committed
18
#Fonction qui va chercher l'image et l'ouvrir ( Avec chemin d'accès absolu ou relatif )
Killian Henry's avatar
Killian Henry committed
19 20 21
def ouvrir_image():
    def essai():
        try:
Killian Henry's avatar
Killian Henry committed
22
            global imagemodèle
Killian Henry's avatar
Killian Henry committed
23 24
            global chemin_image
            global image
25
            chemin_image = entree_chemin.get()
Killian Henry's avatar
Killian Henry committed
26
            image = Image.open(chemin_image)
Killian Henry's avatar
Killian Henry committed
27 28
            imagemodèle = Image.open(chemin_image)
            image.show() # Afficher l'image
Killian Henry's avatar
Killian Henry committed
29
        except FileNotFoundError:
Killian Henry's avatar
Killian Henry committed
30
            tk.Label(fen, text="Le chemin est incorrect").pack()
Killian Henry's avatar
Killian Henry committed
31 32

    global chemin_image
Killian Henry's avatar
Killian Henry committed
33 34 35
    fen = tk.Tk()
    fen.title("traitement image")
    lbl = tk.Label(fen, text="Entrez le chemin")
Killian Henry's avatar
Killian Henry committed
36
    lbl.pack()
Killian Henry's avatar
Killian Henry committed
37
    entree_chemin = tk.Entry(fen)
Killian Henry's avatar
Killian Henry committed
38
    entree_chemin.pack()
Killian Henry's avatar
Killian Henry committed
39 40 41 42 43 44 45 46 47 48 49 50 51
    tk.Button(fen, text="Valider",fg = "green", command = essai).pack(side=tk.LEFT)
    tk.Button(fen, text="Quitter",fg = "red", command = fen.destroy).pack(side=tk.LEFT)
    
    
    fen.mainloop()
#Fonction qui va afficher l'image
 
def afficher_image():  
    global imagemodèle
    if imagemodèle== None:
        fen = tk.Tk()
        fen.title("traitement image")
        tk.Label(fen, text="Entrez le chemin").pack() # ajout graphique
Killian Henry's avatar
Killian Henry committed
52
    else:
Killian Henry's avatar
Killian Henry committed
53
        imagemodèle.show()
Killian Henry's avatar
Killian Henry committed
54
        stocker_data_image()
Killian Henry's avatar
Killian Henry committed
55 56 57 58
 
 
#Fonction qui va afficher les informations concernant l'image (mode colorimétrique, format de l'image, dimensions)
 
Killian Henry's avatar
Killian Henry committed
59 60 61 62 63 64 65 66 67
def afficher_infos_image():
    global chemin_image
    varA = *image.size,
    root = tk.Tk()
    root.title("traiment image")
    tk.Label(root, text="Propriétés de l'image : " + str(chemin_image)).pack()
    tk.Label(root, text="     Mode colorimétrique :" + str(image.mode)).pack()
    tk.Label(root, text="     Format de l'image :" + (image.format)).pack()
    tk.Label(root, text="     Dimensions : " + str(varA)).pack()
Killian Henry's avatar
Killian Henry committed
68 69 70 71 72 73
    tk.Button(root, text="Retour",fg = "red",  command = root.destroy).pack()
     
    
   
#Fonction qui va inverser la couleur pour chaque pixel ( Boucles ? // Créer la fonction et ne pas se baser sur celles déjà existante )
 
Killian Henry's avatar
Killian Henry committed
74
def negatif_couleur():
Killian Henry's avatar
Killian Henry committed
75
    (largeur, hauteur)= image.size
Killian Henry's avatar
Killian Henry committed
76 77
    for x in range(largeur):
                for y in range(hauteur):
Killian Henry's avatar
Killian Henry committed
78 79 80
                    (rouge,vert,bleu) = image.getpixel((x,y))
                    (rouge,vert,bleu) = (255-rouge,255-vert,255-bleu)
                    image.putpixel((x,y),(rouge,vert,bleu))
Killian Henry's avatar
Killian Henry committed
81
    image.show()
Killian Henry's avatar
Killian Henry committed
82 83 84
 
#Fonction qui va faire pivoter l'ensemble de l'image (RGB->RBG // RGB->BGR // RGB->GBR // RBG->GRB)
 
Killian Henry's avatar
Killian Henry committed
85
def rotation_couleurs():
Killian Henry's avatar
Killian Henry committed
86
    (largeur, hauteur)= image.size
Killian Henry's avatar
Killian Henry committed
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
    def rgb():
        image.show()
    def bgr():
        for x in range(largeur): #Parcourir la largeur de l'image
                for y in range(hauteur):#Parcourir la longueur de l'image
                    (rouge,vert,bleu) = image.getpixel((x,y)) #Récupérer les pixels (x,y) et les stocker dans une liste
                    (rouge,vert,bleu) = (bleu,vert,rouge)
                    image.putpixel((x,y),(rouge,vert,bleu))
        image.show()
    def grb():
        for x in range(largeur):
                for y in range(hauteur):
                    (rouge,vert,bleu) = image.getpixel((x,y))
                    (rouge,vert,bleu) = (vert,bleu,rouge)
                    image.putpixel((x,y),(rouge,vert,bleu))
        image.show()
    def gbr():
        for x in range(largeur):
                for y in range(hauteur):
                    (rouge,vert,bleu) = image.getpixel((x,y))
                    (rouge,vert,bleu) = (vert,bleu,rouge)
                    image.putpixel((x,y),(rouge,vert,bleu))
        image.show()
    root = tk.Tk()

    root.title("traiment image")
    tk.Label(root, text="Quel mode de rotation colorimétrique voulez-vous ?").pack()
    tk.Button(root, text="       - RGB - Rotation RGB->RBG", command = rgb).pack()
    tk.Button(root, text="       - BGR - Rotation RGB->BGR", command = bgr).pack()
    tk.Button(root, text="       - GBR - Rotation RGB->GBR", command = grb).pack()
    tk.Button(root, text="       - GRB - Rotation RGB->GRB", command = grb).pack()
    tk.Button(root, text="Retour",fg = "red",  command = root.destroy).pack()


Killian Henry's avatar
Killian Henry committed
121
    
Killian Henry's avatar
Killian Henry committed
122
#Fonction qui va faire pivoter l'ensemble de l'image (-90°,90° et 180°)
Killian Henry's avatar
Killian Henry committed
123
 
Killian Henry's avatar
Killian Henry committed
124
def rotation_image():
Killian Henry's avatar
Killian Henry committed
125
    def plus90():            
Killian Henry's avatar
Killian Henry committed
126 127 128
        angle = 90
        image2 = image.rotate(angle)
        image2.show()
Killian Henry's avatar
Killian Henry committed
129
        global image2
Killian Henry's avatar
Killian Henry committed
130
        fen = tk.Tk()
Killian Henry's avatar
Killian Henry committed
131
        fen.title("traiment image")
Killian Henry's avatar
Killian Henry committed
132
        tk.Label(fen, text="Voulez vous sauvegarder cette image ?").pack()
Killian Henry's avatar
Killian Henry committed
133 134 135
        tk.Button(fen, text="Oui", command = save2).pack()
        
    def moins90():         
Killian Henry's avatar
Killian Henry committed
136 137 138
        angle = -90
        image2 = image.rotate(angle)
        image2.show()
Killian Henry's avatar
Killian Henry committed
139
        global image2
Killian Henry's avatar
Killian Henry committed
140
        fen = tk.Tk()
Killian Henry's avatar
Killian Henry committed
141
        fen.title("traiment image")
Killian Henry's avatar
Killian Henry committed
142
        tk.Label(fen, text="Voulez vous sauvegarder cette image ?").pack()
Killian Henry's avatar
Killian Henry committed
143
        tk.Button(fen, text="Oui",fg="blue", command = save2).pack()
Killian Henry's avatar
Killian Henry committed
144

Killian Henry's avatar
Killian Henry committed
145 146
        
    def plus180():
Killian Henry's avatar
Killian Henry committed
147 148 149
        angle = 180
        image2 = image.rotate(angle)
        image2.show()
Killian Henry's avatar
Killian Henry committed
150
        global image2
Killian Henry's avatar
Killian Henry committed
151
        fen = tk.Tk()
Killian Henry's avatar
Killian Henry committed
152
        fen.title("traiment image")
Killian Henry's avatar
Killian Henry committed
153
        tk.Label(fen, text="Voulez vous sauvegarder cette image ?").pack()
Killian Henry's avatar
Killian Henry committed
154 155
        tk.Button(fen, text="Oui",fg="blue", command = save2).pack()
        
Killian Henry's avatar
Killian Henry committed
156
    def angle_choix():
Killian Henry's avatar
Killian Henry committed
157
        def test():
Killian Henry's avatar
Killian Henry committed
158
            global image
Killian Henry's avatar
Killian Henry committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
            global angleimage
            angleimage.get()
            angle2 = angleimage % 360
            image2 = image.rotate(angle2)
            image2.show
        
        global image
        fen2 = tk.Tk()
        fen2.title("traiment image")
        tk.Label(fen2, text="Donnez l'angle").pack()
        angleimage = tk.Entry(fen2)
        angleimage.pack()
        global angleimage
        tk.Button(fen2, text="Valider",fg = "green", command = test).pack()
        fen2.mainloop()
            
            
Killian Henry's avatar
Killian Henry committed
176
    root = tk.Tk()
Killian Henry's avatar
Killian Henry committed
177 178 179 180 181 182 183 184 185
    root.title("traiment image")
    tk.Label(root, text="Quelle diretion de rotation voulez-vous ?")
    
    tk.Button(root, text="Vers la droite", command = moins90).pack()
    
    tk.Button(root, text="Vers la gauche", command = plus90).pack()
    
    tk.Button(root, text="Retourner", command = plus180).pack()
    
Killian Henry's avatar
Killian Henry committed
186
    tk.Button(root, text="Choisir", command = angle_choix).pack()
Killian Henry's avatar
Killian Henry committed
187
    
Killian Henry's avatar
Killian Henry committed
188 189
    tk.Button(root, text="Retour",fg = "red",  command = root.destroy).pack()

Killian Henry's avatar
Killian Henry committed
190 191
    root.mainloop()
        
Killian Henry's avatar
Killian Henry committed
192

Killian Henry's avatar
Killian Henry committed
193 194
 
# Fonction qui permet de définir la direction de rotation physique (Gauche, droite, retourner b )	
Killian Henry's avatar
Killian Henry committed
195 196 197 198 199 200 201 202 203 204 205 206
def symetrie_image():
    def symetrieV():
        SymétrieV = image.transpose(Image.FLIP_LEFT_RIGHT)
        SymétrieV.show()
    def symetrieH():
        SymétrieH = image.transpose(Image.FLIP_TOP_BOTTOM)
        SymétrieH.show()
    root = tk.Tk()
    tk.Label(root, text="Selon quel axe voulez-vous effectuer la symétrie ?").pack()
    frame=tk.Frame(root)
    frame.pack()
    root.title("symétrie image")
Killian Henry's avatar
Killian Henry committed
207 208 209
    tk.Button(frame, text="Symétrie verticale (ou symétrique d'axe horizontal", command = symetrieH).pack()
    tk.Button(frame, text="Symétrie Horizontale (ou symétrique d'axe vertical", command = symetrieV).pack()
    tk.Button(frame, text="Retour",fg = "red", command = root.destroy).pack()
Killian Henry's avatar
Killian Henry committed
210 211 212


    root.mainloop()
Killian Henry's avatar
Killian Henry committed
213 214
#Fonction qui nettoie l'écran ( supprimer tout élements présent sur l'écran = détruire ) 
 
Killian Henry's avatar
Killian Henry committed
215 216
def nettoyer_ecran():
    os.system("cls")
Killian Henry's avatar
Killian Henry committed
217 218 219
 
# Fonction qui arrête le programme(Message d'arrêt)
 
Killian Henry's avatar
Killian Henry committed
220
def quitter_programme():
Killian Henry's avatar
Killian Henry committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    print("Fin du programme, au revoir.")
    exit
    
def save2():
    def sauvegarde():
        nom_image = input_utilisateur.get()
        image3.save(nom_image) #problème
        tk.Label(fen, text="Votre sauvegarde s'est effectuée avec succès !", fg = "green").pack()
        tk.Button(fen, text="Quitter", fg = "red", command = fen.destroy).pack()
    global image2
    image3 = image2
    fen = tk.Tk()
    fen.title("Traiment image")
    label = tk.Label(fen, text="Entrez le nom du fichier")
    label.pack()
    input_utilisateur = tk.Entry(fen)
    input_utilisateur.pack()
    bouton_valider= tk.Button(fen, text="Valider",fg = "green", command = sauvegarde)
    bouton_valider.pack()
    
def save():
    def sauvegarde():
        nom_image = input_utilisateur.get()
        image2.save(nom_image) #problème
        tk.Label(fen, text="Votre sauvegarde s'est effectuée avec succès !", fg = "green").pack()
        tk.Button(fen, text="Quitter", fg = "red", command = fen.destroy).pack()
    global image
    image2 = image
    fen = tk.Tk()
    fen.title("Traiment image")
    label = tk.Label(fen, text="Entrez le nom du fichier")
    label.pack()
    input_utilisateur = tk.Entry(fen)
    input_utilisateur.pack()
    bouton_valider= tk.Button(fen, text="Valider",fg = "green", command = sauvegarde)
    bouton_valider.pack()
    
Killian Henry's avatar
Killian Henry committed
258
def stocker_data_image():
Killian Henry's avatar
Killian Henry committed
259 260
    
    
Killian Henry's avatar
Killian Henry committed
261
    # Open a file
Killian Henry's avatar
Killian Henry committed
262 263
    
    
Killian Henry's avatar
Killian Henry committed
264 265 266 267
    pixel_image = list(image.getdata())
    data = open("pixel_image.txt", "w")
    data.write( "{}".format(pixel_image))
    data.close()
Killian Henry's avatar
Killian Henry committed
268 269 270
 
# Fonction qui crée le menu et le structure (O,A,I,N,C,R,S,Q)
 
Killian Henry's avatar
Killian Henry committed
271 272 273
def menu():
    root = tk.Tk()
    root.title("traiment image")
Killian Henry's avatar
Killian Henry committed
274 275 276 277 278 279 280 281 282 283
    tk.Button(root, text="Ouvrir l'image et l'afficher", command = ouvrir_image).pack()
    tk.Button(root, text="Afficher l'image précédement chargée", command = afficher_image).pack()
    tk.Button(root, text="Afficher les informations concernant l'image", command = afficher_infos_image).pack()
    tk.Button(root, text="Mettre l'image en négatif ", command = negatif_couleur).pack()
    tk.Button(root, text="Effectuer la rotation des couleurs de l'image", command = rotation_couleurs).pack()
    tk.Button(root, text="Effectuer la rotation physique de l'image ", command = rotation_image).pack()
    tk.Button(root, text="Effectuer une transformation symétrique de l'image", command = symetrie_image).pack()
    tk.Button(root, text="Effacer la console ", command = nettoyer_ecran).pack()
    tk.Button(root, text= "Sauvegarder l'image", command = save).pack()
    tk.Button(root, text="Quitter le programme", fg = "red", command = root.destroy).pack()
Killian Henry's avatar
Killian Henry committed
284
    root.mainloop()
Killian Henry's avatar
Killian Henry committed
285 286
    
if __name__ == "__main__":
Killian Henry's avatar
Killian Henry committed
287
    menu()