Home > other >  Python PIllow Tkinter - How to edit a photo twice without losing the previous change?
Python PIllow Tkinter - How to edit a photo twice without losing the previous change?

Time:10-27

How do I edit a photo without losing previous edits? Below is my code. After uploading a photo and pressing the blur button, the photo blurs, but if I then press rotate, the blurring of the photo disappears. How do I keep the result of both effects? Do I have to save the photo to a separate file after using each function?

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename,asksaveasfilename
from PIL import Image, ImageTk, ImageFilter, ImageEnhance, ImageOps
import os



my_w = Tk()
my_w.geometry("1000x1000")  # Size of the window
my_w.title('Photo Editing Tool')


def upload_image():
    global img_path, img
    img_path = filedialog.askopenfilename(initialdir=os.getcwd())
    img = Image.open(img_path)
    img.thumbnail((350, 350))
    img = ImageTk.PhotoImage(img)
    canvas2.create_image(300, 210, image=img)
    canvas2.image=img


def rotate_image():
    global img_path, img
    img = Image.open(img_path)
    img.thumbnail((350, 350))
    img = img.rotate(90)
    img = ImageTk.PhotoImage(img)
    canvas2.create_image(300, 210, image=img)
    canvas2.image = img


def blur():
    global img_path, img
    img = Image.open(img_path)
    img.thumbnail((350, 350))
    img = img.filter(ImageFilter.BLUR)
    img = ImageTk.PhotoImage(img)
    canvas2.create_image(300, 210, image=img)
    canvas2.image = img


canvas2 = Canvas(my_w, width="600", height="420", relief=RIDGE, bd=2)
canvas2.place(x=15, y=150)

btn_select = Button(my_w, text="Select Image", bg='white', fg='black',
              font=('ariel 15 bold'), relief=GROOVE, command=upload_image)
btn_select.place(x=100, y=595)

btn_blur = Button(my_w, text="Blur Image", bg='white',fg='black',
                    font=('ariel 15 bold'), relief=GROOVE, command=blur)
btn_blur.place(x=250, y=595)

btn_rotate = Button(my_w, text="Rotate Image", bg='white',fg='black',
                    font=('ariel 15 bold'), relief=GROOVE, command=rotate_image)
btn_rotate.place(x=400, y=595)

my_w.mainloop()

CodePudding user response:

the reason why the previous change is lost is because each of your functions is reading the image again from disk, and while saving it back to disk temporarily is what some image processing applications do to prevent loss of work on power outage, you can simply work from memory for the application by hand.

  1. define a variable to hold the image, in you case it is img
  2. when user selects image the image will be loaded from disk and saved in that variable
  3. when user applies an effect, it will be applied to the image in memory, the image won't be read from disk again.
  4. before drawing make another variable the PhotoImage from img and draw it to screen, but don't overwrite img while drawing.
  5. if you add a save button, it only have to save img as it will already be in memory.
img = None
img_path = None

def upload_image():
    global img_path, img
    img_path = filedialog.askopenfilename(initialdir=os.getcwd())
    img = Image.open(img_path)
    img.thumbnail((350, 350))
    photo_img = ImageTk.PhotoImage(img)
    canvas2.create_image(300, 210, image=photo_img)
    canvas2.image = photo_img


def rotate_image():
    global img
    img = img.rotate(90)
    photo_img = ImageTk.PhotoImage(img)
    canvas2.create_image(300, 210, image=photo_img)
    canvas2.image = photo_img


def blur():
    global img
    img = img.filter(ImageFilter.BLUR)
    photo_img = ImageTk.PhotoImage(img)
    canvas2.create_image(300, 210, image=photo_img)
    canvas2.image = photo_img
  • Related