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
| from PIL import Image as Im
import os
from tkinter import Entry, Label, Tk
from tkinter import Canvas, PhotoImage, Button
def recup():
"""
récupération du nom de fichier et de l'extension
ouverture de l'image
"""
nom_fich=entr1.get()
nom_ext = entr2.get()
ext1 = '.jpg'
ext2 = '.gif'
source_jpg = nom_fich + '.' + nom_ext
source_gif = nom_fich + ext2
cible_jpg = nom_fich + 'conv' + ext1
cible_gif = nom_fich + 'conv' + ext2
global mon_image, largeur, hauteur
mon_image = Im.open(source_jpg)
largeur,hauteur = mon_image.size
def jpg_gif(nom1,nom2):
"""
conversion de format, par exemple jpg en gif
"""
os.system('convert %s %s' % (nom1,nom2))
def negatif_couleur(image1):
"""
conversion couleur en négatif couleur, donc avec 3 canaux
"""
image2 = Im.new('RGB',(largeur,hauteur))
for colonne in range(largeur):
for ligne in range(hauteur):
pixel=image1.getpixel((colonne,ligne))
rouge = 255 - pixel[0]
vert = 255 - pixel[1]
bleu = 255 - pixel[2]
image2.putpixel((colonne,ligne),(rouge,vert,bleu))
return(image2)
def conv_negatif_couleur():
""" conversion négatif couleur puis conversion de format
pour avoir les aperçus dans tkinter"""
res = negatif_couleur(mon_image)
res.save(cible_jpg)
jpg_gif(source_jpg,source_gif)
jpg_gif(cible_jpg,cible_gif)
def rafraichissement():
"""
rafarichissement des images source et cible dans les fenêtres de visualisation
"""
global cible_gif, source_gif, photo1,image1,photo2,image2,can1,can2
if os.path.isfile(source_gif) == True:
photo1 = PhotoImage(file = source_gif)
image1 = can1.create_image(80, 80, image =photo1)
if os.path.isfile(cible_gif) == True:
photo2 = PhotoImage(file = cible_gif)
image2 = can2.create_image(80, 80, image =photo2)
can1 = Canvas(fen1, width =160, height =160, bg ='white')
can1.grid(row =1, column =3, rowspan =3, padx =10, pady =5)
can2 = Canvas(fen1, width =160, height =160, bg ='white')
can2.grid(row =1, column =4, rowspan =3, padx =10, pady =5)
fen1 = Tk()
txt1 = Label(fen1, text ='Nom de fichier (sans extension)')
entr1 = Entry(fen1)
txt1.grid(row =1, column=1, sticky ='EW')
entr1.grid(row =1, column =2)
txt2 = Label(fen1, text ='Extension')
entr2 = Entry(fen1)
txt2.grid(row =2, column=1, sticky ='EW')
entr2.grid(row =2, column =2)
# création d'un widget 'Canvas' contenant les images gif :
can1 = Canvas(fen1, width =160, height =160, bg ='white')
can1.grid(row =1, column =3, rowspan =3, padx =10, pady =5)
can2 = Canvas(fen1, width =160, height =160, bg ='white')
can2.grid(row =1, column =4, rowspan =3, padx =10, pady =5)
## Bouton pour convertir
Button(fen1,text='Acquisition',command=recup).grid(row=3 , column=1)
## Bouton pour convertir
Button(fen1,text='Négatif couleur',command=conv_negatif_couleur).grid(row=4 , column=1)
## Raffraichissement
Button(fen1,text='Rafraichissement',command=rafraichissement).grid(row=4 , column=3,columnspan=2)
# fermeture
Button(fen1,text='Quitter',command=fen1.destroy).grid(row=5, column=1)
# démarrage :
fen1.mainloop() |
Partager