Cette fois, j'écrirai un article sur Tkinter.
J'ai créé une interface graphique qui divise une image en quatre verticalement et horizontalement et enregistre l'image divisée.
Image terminée
Cette fois, je voulais cliquer et enregistrer l'image, donc l'image est affichée sur le bouton Passez à une autre image dans le dossier avec «Image précédente» et «Image suivante» ci-dessous.
python
self.Button = tkinter.Button(master,width=sep,height=sep,text='Non sélectionné') #Générez un bouton.
self.Button["bd"] = "7"
self.Button["compound"] = "top"
self.Button["command"]=dvd_clicked
self.Button["image"]= self.image_tk
self.Button.grid(row=0, column=0)
composé = Position des caractères lorsque l'image est placée commande = événement lorsque le bouton est enfoncé image = Image à afficher
def dvd_clicked():
self.Button["text"] = "Choix"
self.Button["fg"] = "RED"
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0.jpg', quality=95)
Ici, l'image est enregistrée lorsque chaque bouton est cliqué. Afin de sauvegarder chaque pièce, j'ai ajouté des informations de localisation telles que 0_1 et enregistré le fichier.
Après avoir sélectionné la zone sélectionnée, le texte sera "sélectionné" en rouge.
Si vous appuyez sur "Image précédente" ou "Image suivante", chaque image sera mise à jour.
import tkinter as tk
import tkinter
import cv2
from PIL import Image, ImageTk
import os
import glob
os.chdir('./')
class Application(tk.Frame):
def __init__(self,master):
super().__init__(master)
master.title("Fenêtre de base")
global i
self.window = []
self.user = []
self.seq=0
i=0
self.size=64 #Taille divisée
sep = 70#Détermination de la taille du bouton
self.Clp=[0 ,self.size*1,self.size*2,self.size*3,self.size*4]
def dvd_clicked():
self.Button["text"] = "Choix"
self.Button["fg"] = "RED"
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0.jpg', quality=95)
def dvd_clicked0_1():
self.Button0_1["text"] = "Choix"
self.Button0_1["fg"] = "RED"
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[0],self.Clp[2],self.Clp[1]))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0_1.jpg', quality=95)
def dvd_clicked0_2():
self.Button0_2["text"] = "Choix"
self.Button0_2["fg"] = "RED"
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[0],self.Clp[3],self.Clp[1]))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0_2.jpg', quality=95)
def dvd_clicked0_3():
self.Button0_3["text"] = "Choix"
self.Button0_3["fg"] = "RED"
self.im_crop = self.image_pil.crop((192, 0, 256, 64))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'0_3.jpg', quality=95)
def dvd_clicked1():
self.Button1["text"] = "Choix"
self.Button1["fg"] = "RED"
self.im_crop = self.image_pil.crop((0, 64, 64, 128))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'1.jpg', quality=95)
def dvd_clicked1_1():
self.Button1_1["text"] = "Choix"
self.Button1_1["fg"] = "RED"
self.im_crop = self.image_pil.crop((64, 64, 128, 128))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'1_1.jpg', quality=95)
def dvd_clicked1_2():
self.Button1_2["text"] = "Choix"
self.Button1_2["fg"] = "RED"
self.im_crop = self.image_pil.crop((128, 64, 192, 128))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'1_2.jpg', quality=95)
def dvd_clicked1_3():
self.Button1_3["text"] = "Choix"
self.Button1_3["fg"] = "RED"
self.im_crop = self.image_pil.crop((192, 64, 256, 128))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'1_3.jpg', quality=95)
def dvd_clicked2():
self.Button2["text"] = "Choix"
self.Button2["fg"] = "RED"
self.im_crop = self.image_pil.crop((0, 128, 64, 192))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'2.jpg', quality=95)
def dvd_clicked2_1():
self.Button2_1["text"] = "Choix"
self.Button2_1["fg"] = "RED"
self.im_crop = self.image_pil.crop((64, 128, 128, 192))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'2_1.jpg', quality=95)
def dvd_clicked2_2():
self.Button2_2["text"] = "Choix"
self.Button2_2["fg"] = "RED"
self.im_crop = self.image_pil.crop((128, 128, 192, 192))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'2_2.jpg', quality=95)
def dvd_clicked2_3():
self.Button2_3["text"] = "Choix"
self.Button2_3["fg"] = "RED"
self.im_crop = self.image_pil.crop((192, 128, 256, 192))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'2_3.jpg', quality=95)
def dvd_clicked3():
self.Button3["text"] = "Choix"
self.Button3["fg"] = "RED"
self.im_crop = self.image_pil.crop((0, 192, 64, 256))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'3.jpg', quality=95)
def dvd_clicked3_1():
self.Button3_1["text"] = "Choix"
self.Button3_1["fg"] = "RED"
self.im_crop = self.image_pil.crop((64, 192, 128, 256))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'3_1.jpg', quality=95)
def dvd_clicked3_2():
self.Button3_2["text"] = "Choix"
self.Button3_2["fg"] = "RED"
self.im_crop = self.image_pil.crop((128, 192, 192, 256))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'3_2.jpg', quality=95)
def dvd_clicked3_3():
self.Button3_3["text"] = "Choix"
self.Button3_3["fg"] = "RED"
self.im_crop = self.image_pil.crop((192, 192, 256, 256))
self.im_crop.save('moji/Clp'+str(basename_without[i])+'3_3.jpg', quality=95)
def dvd():
global i
"""Génération de nom de fichier"""
i+=1
if(i>=len(filename)):
i=0
self.Button.grid_remove()
#filename[i] = "tihou.jpg "
image_bgr = cv2.imread(filename[i])
image_bgr = cv2.resize(image_bgr , (256, 256))
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) #Comme imread est BGR, il est converti en RVB
self.image_pil = Image.fromarray(image_rgb) #Conversion du format RVB au format PIL
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
self.image_tk = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[0],self.Clp[2],self.Clp[1]))
self.image_tk0_1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[0],self.Clp[3],self.Clp[1]))
self.image_tk0_2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[0],self.Clp[4],self.Clp[1]))
self.image_tk0_3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[1],self.Clp[1],self.Clp[2]))
self.image_tk1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[1],self.Clp[2],self.Clp[2]))
self.image_tk1_1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[1],self.Clp[3],self.Clp[2]))
self.image_tk1_2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[1],self.Clp[4],self.Clp[2]))
self.image_tk1_3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[2],self.Clp[1],self.Clp[3]))
self.image_tk2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[2],self.Clp[2],self.Clp[3]))
self.image_tk2_1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[2],self.Clp[3],self.Clp[3]))
self.image_tk2_2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[2],self.Clp[4],self.Clp[3]))
self.image_tk2_3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[3],self.Clp[1],self.Clp[4]))
self.image_tk3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[3],self.Clp[2],self.Clp[4]))
self.image_tk3_1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[3],self.Clp[3],self.Clp[4]))
self.image_tk3_2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.im_crop = self.image_pil.crop((self.Clp[3],self.Clp[3],self.Clp[4],self.Clp[4]))
self.image_tk3_3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
#Mettez à jour le bouton.
self.Button["image"]= self.image_tk
self.Button0_1["image"]= self.image_tk0_1
self.Button0_2["image"]= self.image_tk0_2
self.Button0_3["image"]= self.image_tk0_3
#Mettez à jour le bouton.
self.Button1["image"]= self.image_tk1
self.Button1_1["image"]= self.image_tk1_1
self.Button1_2["image"]= self.image_tk1_2
self.Button1_3["image"]= self.image_tk1_3
#Mettez à jour le bouton.
self.Button2["image"]= self.image_tk2
self.Button2_1["image"]= self.image_tk2_1
self.Button2_2["image"]= self.image_tk2_2
self.Button2_3["image"]= self.image_tk2_3
#Mettez à jour le bouton.
self.Button3["image"]= self.image_tk3
self.Button3.grid(row=0, column=0)
self.Button3_1["image"]= self.image_tk3_1
self.Button3_2["image"]= self.image_tk3_2
self.Button3_3["image"]= self.image_tk3_3
self.Button.grid(row=0, column=0)
self.Button0_1.grid(row=0, column=1)
self.Button0_2.grid(row=0, column=2)
self.Button0_3.grid(row=0, column=3)
self.Button1.grid(row=1, column=0)
self.Button1_1.grid(row=1, column=1)
self.Button1_2.grid(row=1, column=2)
self.Button1_3.grid(row=1, column=3)
self.Button2.grid(row=2, column=0)
self.Button2_1.grid(row=2, column=1)
self.Button2_2.grid(row=2, column=2)
self.Button2_3.grid(row=2, column=3)
self.Button3.grid(row=3, column=0)
self.Button3_1.grid(row=3, column=1)
self.Button3_2.grid(row=3, column=2)
self.Button3_3.grid(row=3, column=3)
basename = []
basename_without=[]
filename =glob.glob('*.jpg')
for x in range(len(filename)):
#nom de fichier(Avec extension)Pour le chargement d'image
basename1 = os.path.basename(filename[x])
basename.append(basename1)
#nom de fichier(Aucune extension)Utilisé lors de l'enregistrement d'images
basename_without_ext = os.path.splitext(os.path.basename(filename[x]))[0]
basename_without.append(basename_without_ext)
#filename[i] = "tihou.jpg "
image_bgr = cv2.imread(filename[i])
image_bgr = cv2.resize(image_bgr , (256, 256))
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) #Comme imread est BGR, il est converti en RVB
self.image_pil = Image.fromarray(image_rgb) #Conversion du format RVB au format PIL
self.im_crop = self.image_pil.crop((self.Clp[0],self.Clp[0],self.Clp[1],self.Clp[1]))
self.image_tk = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
#Placez le bouton.
self.Button = tkinter.Button(master,width=sep,height=sep,text='Non sélectionné') #Générez un bouton.
self.Button["bd"] = "7"
self.Button["compound"] = "top"
self.Button["command"]=dvd_clicked
self.Button["image"]= self.image_tk
self.Button.grid(row=0, column=0)
self.im_crop = self.image_pil.crop((self.Clp[1],self.Clp[0],self.Clp[2],self.Clp[1]))
self.image_tk0_1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button0_1= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné',image = self.image_tk0_1) #Générez un bouton.
self.Button0_1["bd"] = "7"
self.Button0_1["compound"] = "top"
self.Button0_1["command"]=dvd_clicked0_1
self.Button0_1.grid(row=0, column=1)
self.im_crop = self.image_pil.crop((self.Clp[2],self.Clp[0],self.Clp[3],self.Clp[1]))
self.image_tk0_2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button0_2= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné',image = self.image_tk0_2) #Générez un bouton.
self.Button0_2["bd"] = "7"
self.Button0_2["compound"] = "top"
self.Button0_2["command"]=dvd_clicked0_2
self.Button0_2.grid(row=0, column=2)
canvas0_3 = tkinter.Canvas(master, width=sep, height=sep) #Création de toiles
#canvas.pack()
self.im_crop = self.image_pil.crop((192, 0, 256, 64))
self.image_tk0_3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button0_3= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné',image = self.image_tk0_3) #Générez un bouton.
self.Button0_3["bd"] = "7"
self.Button0_3["compound"] = "top"
self.Button0_3["command"]=dvd_clicked0_3
self.Button0_3.grid(row=0, column=3)
self.im_crop = self.image_pil.crop((0, 64, 64, 128))
self.image_tk1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button1= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné' ,image = self.image_tk1) #Générez un bouton.
self.Button1["bd"] = "7"
self.Button1["compound"] = "top"
self.Button1["command"]=dvd_clicked1
self.Button1.grid(row=1, column=0)
self.im_crop = self.image_pil.crop((64, 64, 128, 128))
self.image_tk1_1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button1_1= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné' ,image = self.image_tk1_1) #Générez un bouton.
self.Button1_1["bd"] = "7"
self.Button1_1["compound"] = "top"
self.Button1_1["command"]=dvd_clicked1_1
self.Button1_1.grid(row=1, column=1)
self.im_crop = self.image_pil.crop((128, 64, 192, 128))
self.image_tk1_2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button1_2= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné' ,image = self.image_tk1_2) #Générez un bouton.
self.Button1_2["bd"] = "7"
self.Button1_2["compound"] = "top"
self.Button1_2["command"]=dvd_clicked1_2
self.Button1_2.grid(row=1, column=2)
canvas1_3 = tkinter.Canvas(master, width=sep, height=sep) #Création de toiles
#canvas.pack()
self.im_crop = self.image_pil.crop((192 ,64, 256, 128))
self.image_tk1_3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button1_3= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné',image = self.image_tk1_3) #Générez un bouton.
self.Button1_3["bd"] = "7"
self.Button1_3["compound"] = "top"
self.Button1_3["command"]=dvd_clicked1_3
self.Button1_3.grid(row=1, column=3)
self.im_crop = self.image_pil.crop((0, 128, 64, 192))
self.image_tk2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button2= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné' ,image = self.image_tk2) #Générez un bouton.
self.Button2["bd"] = "7"
self.Button2["compound"] = "top"
self.Button2["command"]=dvd_clicked2
self.Button2.grid(row=2, column=0)
self.im_crop = self.image_pil.crop((64, 128, 128, 192))
self.image_tk2_1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button2_1= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné' ,image = self.image_tk2_1) #Générez un bouton.
self.Button2_1["bd"] = "7"
self.Button2_1["compound"] = "top"
self.Button2_1["command"]=dvd_clicked2_1
self.Button2_1.grid(row=2, column=1)
self.im_crop = self.image_pil.crop((128, 128, 192, 192))
self.image_tk2_2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button2_2= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné' ,image = self.image_tk2_2) #Générez un bouton.
self.Button2_2["bd"] = "7"
self.Button2_2["compound"] = "top"
self.Button2_2["command"]=dvd_clicked2_2
self.Button2_2.grid(row=2, column=2)
self.im_crop = self.image_pil.crop((192, 128, 256, 192))
self.image_tk2_3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button2_3= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné',image = self.image_tk2_3) #Générez un bouton.
self.Button2_3["bd"] = "7"
self.Button2_3["compound"] = "top"
self.Button2_3["command"]=dvd_clicked2_3
self.Button2_3.grid(row=2, column=3)
self.im_crop = self.image_pil.crop((0, 192, 64, 256))
self.image_tk3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button3= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné',image = self.image_tk3) #Générez un bouton.
self.Button3["bd"] = "7"
self.Button3["compound"] = "top"
self.Button3["command"]=dvd_clicked3
self.Button3.grid(row=3, column=0)
self.im_crop = self.image_pil.crop((64, 192, 128, 256))
self.image_tk3_1 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button3_1= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné' ,image = self.image_tk3_1) #Générez un bouton.
self.Button3_1["bd"] = "7"
self.Button3_1["compound"] = "top"
self.Button3_1["command"]=dvd_clicked3_1
self.Button3_1.grid(row=3, column=1)
self.im_crop = self.image_pil.crop((128, 192, 192, 256))
self.image_tk3_2 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button3_2= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné' ,image = self.image_tk3_2) #Générez un bouton.
self.Button3_2["bd"] = "7"
self.Button3_2["compound"] = "top"
self.Button3_2["command"]=dvd_clicked3_2
self.Button3_2.grid(row=3, column=2)
self.im_crop = self.image_pil.crop((192, 192, 256, 256))
self.image_tk3_3 = ImageTk.PhotoImage(self.im_crop) #Convertir au format ImageTk
self.Button3_3= tkinter.Button(master,width=sep,height=sep,text='Non sélectionné' ,image = self.image_tk3_3) #Générez un bouton.
self.Button3_3["bd"] = "7"
self.Button3_3["compound"] = "top"
self.Button3_3["command"]=dvd_clicked3_3
self.Button3_3.grid(row=3, column=3)
#Placez l'étiquette.
label1 = tkinter.Label(master,text='Veuillez sélectionner une zone de stockage')
label1["font"] = ("",18)
label1["height"] = 2
label1.grid(row=4, column=0,columnspan=4)
self.SButton1= tkinter.Button(master,text='<<Image précédente' ) #Générez un bouton.
self.SButton1["bd"] = "7"
self.SButton1.grid(row=5, column=0,columnspan=2)
self.SButton2= tkinter.Button(master,text='Image suivante>>' ) #Générez un bouton.
self.SButton2["bd"] = "7"
self.SButton2["command"]=dvd
self.SButton2.grid(row=5, column=2,columnspan=2)
def main():
win = tk.Tk()
app = Application(win)
app.mainloop()
if __name__ == '__main__':
main()