from tkinter import Tk, Canvas, PhotoImage, Button, LEFT, TOP, BOTTOM, RIGHT, X, Y, NW, NE, SW, SE from tkinter import filedialog from PIL import Image, ImageTk class BildbearbeitungApp: def __init__(self, master): self.master = master master.title("Bildbearbeitung") self.canvas = Canvas(master, width=600, height=400, bg="white") self.canvas.pack(fill=BOTH, expand=True) self.bild = None self.bild_tk = None # Buttons self.button_laden = Button(master, text="Bild laden", command=self.laden) self.button_laden.pack(side=LEFT) self.button_speichern = Button(master, text="Bild speichern", command=self.speichern) self.button_speichern.pack(side=RIGHT) # Bildbearbeitungs-Buttons self.button_grau = Button(master, text="Graustufen", command=self.grau) self.button_grau.pack(side=BOTTOM) self.button_spiegeln = Button(master, text="Spiegeln", command=self.spiegeln) self.button_spiegeln.pack(side=BOTTOM) self.button_drehen = Button(master, text="Drehen", command=self.drehen) self.button_drehen.pack(side=BOTTOM) # Text "byy Solra" self.text_by_solra = self.canvas.create_text(10, 380, anchor=SW, text="byy Solra", fill="gray") def laden(self): # Datei-Dialog zum Laden des Bildes filepath = filedialog.askopenfilename( initialdir="/", title="Bild auswählen", filetypes=(("Bilddateien", "*.jpg *.jpeg *.png *.gif"), ("alle Dateien", "*.*")) ) if filepath: self.bild = Image.open(filepath) self.bild_tk = ImageTk.PhotoImage(self.bild) self.canvas.create_image(0, 0, anchor=NW, image=self.bild_tk) self.canvas.config(width=self.bild.width, height=self.bild.height) def speichern(self): if self.bild is not None: # Datei-Dialog zum Speichern des Bildes filepath = filedialog.asksaveasfilename( defaultextension=".jpg", filetypes=(("JPEG-Datei", "*.jpg"), ("PNG-Datei", "*.png")) ) if filepath: self.bild.save(filepath) def grau(self): if self.bild is not None: self.bild = self.bild.convert("L") # Graustufen-Umwandlung self.bild_tk = ImageTk.PhotoImage(self.bild) self.canvas.create_image(0, 0, anchor=NW, image=self.bild_tk) def spiegeln(self): if self.bild is not None: self.bild = self.bild.transpose(Image.FLIP_LEFT_RIGHT) # Spiegeln self.bild_tk = ImageTk.PhotoImage(self.bild) self.canvas.create_image(0, 0, anchor=NW, image=self.bild_tk) def drehen(self): if self.bild is not None: self.bild = self.bild.rotate(90) # Drehen um 90 Grad self.bild_tk = ImageTk.PhotoImage(self.bild) self.canvas.create_image(0, 0, anchor=NW, image=self.bild_tk) root = Tk() app = BildbearbeitungApp(root) root.mainloop()