Bonjour,

J'ai réalisé quelques opérations de traitement d'image et j'aimerais passer cela en tkinter.
tkinter ne peut pas afficher les jpg, d'après ce que j'ai compris.
Je passe donc les images en gif

Dans le code suivant :
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
76
77
78
79
80
81
82
83
from PIL import Image as Im
import os
from tkinter import Entry, Label, Tk
from tkinter import Canvas, PhotoImage, Button
 
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)
 
 
fen1 = Tk()
 
# création de widgets 'Label' et 'Entry' :
txt1 = Label(fen1, text ='Nom de fichier (sans extension)')
entr1 = Entry(fen1)
# Mise en page entr1
txt1.grid(row =1, column=1, sticky ='EW')
entr1.grid(row =1, column =2)
 
#champ 2
txt2 = Label(fen1, text ='Extension')
entr2 = Entry(fen1)
# Mise en page entr2
txt2.grid(row =2, column=1, sticky ='EW')
entr2.grid(row =2, column =2)
 
#nom_fich=entr1.get()
#nom_ext = entr2.get()
nom_fich = 'lena'
nom_ext = 'jpg'
ext1 = '.jpg'
ext2 = '.gif'
nom_fichier = nom_fich + '.' + nom_ext
source_jpg = nom_fich + ext1
source_gif = nom_fich + ext2
cible_jpg = nom_fich + 'conv' + ext1
cible_gif = nom_fich + 'conv' + ext2
 
mon_image = Im.open(source_jpg)
largeur,hauteur = mon_image.size
 
## Bouton pour convertir
Button(fen1,text='Négatif couleur',command=conv_negatif_couleur).grid(row=3 , column=1) 
 
# création d'un widget 'Canvas' contenant les images gif :
can1 = Canvas(fen1, width =160, height =160, bg ='white')
photo1 = PhotoImage(file = source_gif)
image1 = can1.create_image(80, 80, image =photo1)
can2 = Canvas(fen1, width =160, height =160, bg ='white')
photo2 = PhotoImage(file = cible_gif)
image2 = can2.create_image(80, 80, image =photo2)
 
can1.grid(row =1, column =3, rowspan =3, padx =10, pady =5)
can2.grid(row =1, column =4, rowspan =3, padx =10, pady =5)
 
# fermeture
Button(fen1,text='Quitter',command=fen1.destroy).grid(row=4, column=1) 
# démarrage :
fen1.mainloop()
Tout fonctionne.
Lorsque je commente / décommente les lignes 51 à 54 :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
#nom_fich=entr1.get()
#nom_ext = entr2.get()
nom_fich = 'lena'
nom_ext = 'jpg'
cela ne fonctionne plus.

J'ai le message suivant (ligne 69 non réelle, en fait 63) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
  File "/media/marco3/Données 1,9To/Informatique/python_marco/tkinter/tk-photo_inter.py", line 69, in <module>
    mon_image = Im.open(source_jpg)
 
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 1991, in open
    fp = builtins.open(fp, "rb")
 
FileNotFoundError: [Errno 2] No such file or directory: '.jpg'
Je subodore un souci de variable globale mais j'ai du mal à me dépatouiller.
Si quelqu'un peut me donner un coup de main, sachant que ma connaissance de tkinter est proche du niveau zéro...
Bon dimanche