Bonjour tout le monde,
depuis quelques jours j’apprends à coder en python mais je sais pas pourquoi mon éditeur de photo ne fonctionne pas. Merci au futur magicien ou magicienne qui m’aidera à débloquer cette situation
voici l’affichage de mon écran :
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk, ImageOps
class PhotoEditor:
def __init__(self, root):
self.root = root
self.root.title("Photo Editor")
self.canvas = tk.Canvas(self.root, width=400, height=400)
self.canvas.pack()
self.menu_bar = tk.Menu(self.root)
self.root.config(menu=self.menu_bar)
self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
self.menu_bar.add_cascade(label= "File", menu=self.file_menu)
self.file_menu.add_command(label="Open", command=self.open_image)
self.file_menu.add_separator()
self.file_menu.add_command(label="Exit", command=self.root.quit)
self.edit_menu = tk.Menu(self.menu_bar, tearoff=0)
self.menu_bar.add_cascade(label="Edit", menu=self.edit_menu)
self.edit_menu.add_command(label="Grayscale", command=self.convert_to_grayscale)
self.image = None
self.processed_image = None
def open_image(self):
file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.png;*.jpg;*.jpeg*.gif;*.bmp")])
if file_path:
image = Image.open(file_path)
self.image = Image.PhotoImage(image)
self.processed_image = image.copy()
self.canvas.create_image(0,0,anchor=tk.NW, image=self.image)
def convert_to_grayscale(self):
if self.processed_image:
grayscale_image = ImageOps.grayscale(self.processed_image)
self.image = Image.PhotoImage(grayscale_image)
self.canvas.create_image(0,0,anchor=tk.NW, image = self.image)
if __name__ == "__main__":
root = tk.Tk()
app = PhotoEditor(root)
root.mainloop()
PS C:\Users\CES> & "C:/Program Files/Python312/python.exe" "c:/Users/CES/Desktop/Python courses/PhotoEditor.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python312\Lib\tkinter\__init__.py", line 1967, in __call__
return self.func(*args)
File "c:\Users\CES\Desktop\Python courses\PhotoEditor.py", line 34, in open_image
self.image = Image.PhotoImage(image)
AttributeError: module 'PIL.Image' has no attribute 'PhotoImage'
@Amaury — Mise en forme du code et de l’extrait de terminal.
+0
-0