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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
from Tkinter import *
from tkFileDialog import askopenfilename
from PIL import Image, ImageTk
def parcourir ():
"""
ouvre un fichier image et le dispatche dans
photo originale / photo retouchée
"""
global image_in, image_out
fichier = askopenfilename(
title="Sélectionnez une image",
filetypes=[
("Images", "*.jpg"),
("Images", "*.jpeg"),
("Images", "*.png"),
("Images", "*.gif"),
],
parent=fenetre,
)
if fichier:
image_in = Image.open(fichier)
image_out = image_in.copy()
update_canvas()
# end if
# end def
def update_canvas ():
"""
met à jour l'affichage des images dans le canevas
"""
global photo_in, photo_out
# images en vignettes
image_in.thumbnail((300,300), Image.BICUBIC)
image_out.thumbnail((300,300), Image.BICUBIC)
# conversion images -> photo compatibles Tkinter
photo_in = ImageTk.PhotoImage(image_in)
photo_out = ImageTk.PhotoImage(image_out)
# on efface le canevas
canvas.delete(ALL)
# on remet les photos sur le canevas
canvas.create_image(10, 30, anchor=NW, image=photo_in)
canvas.create_image(330, 30, anchor=NW, image=photo_out)
# texte dans canevas
canvas.create_text(160, 10, anchor=N, text="Originale", fill="white")
canvas.create_text(480, 10, anchor=N, text="Retouchée", fill="white")
# end def
def filtre_originale ():
"""
rétablit image originale dans image retouchée
"""
global image_out
if image_in:
image_out = image_in.copy()
update_canvas()
# end if
# end def
def filtre_sepia ():
"""
transforme une image colorée en sepia
"""
global image_out
if not image_in:
return
# end if
# sous-fonction
def nuancier(r, g, b):
ramp = []
for i in range(256):
ramp.extend((r*i/255, g*i/255, b*i/255))
# end for
return ramp
# end def
# make sepia ramp
# nuance de sepia style "vieillot"
sepia = nuancier(230, 216, 173)
# convert to grayscale
image_out = image_in.convert("L")
# apply sepia palette
image_out.putpalette(sepia)
# convert back to RGB so we can save it as JPEG
# (alternatively, save it in PNG or similar)
image_out = image_out.convert("RGB")
# mise à jour canevas
update_canvas()
# end def
# init variables globales
image_in = None
image_out = None
# création de l'IHM
fenetre = Tk()
fenetre.title("Photo retouche")
fenetre.resizable(width=False, height=False)
canvas = Canvas(fenetre, bg="grey", width=640, height=400)
canvas.grid(row=0, column=0, padx=5, pady=5)
# barre d'outils "filtres"
filtres = Frame(fenetre)
filtres.grid(row=0, column=1, padx=5, pady=5, sticky=N)
Label(filtres, text="Filtres :").pack()
Button(filtres, text="Originale", command=filtre_originale).pack(fill=X)
Button(filtres, text="Sepia", command=filtre_sepia).pack(fill=X)
Button(filtres, text="Filtre?", command=None).pack(fill=X)
Button(filtres, text="Filtre?", command=None).pack(fill=X)
# boutons du bas
Button(
fenetre, text="Parcourir", command=parcourir
).grid(row=1, column=0, padx=5, pady=5)
Button(
fenetre, text="Quitter", command=fenetre.destroy
).grid(row=1, column=1, padx=5, pady=5)
fenetre.mainloop() |
Partager