How to make a simple Image Editor in Python (Python Tkinter)

In this post, you will get the free source code of a simple GUI image editting software, made in Tkinter, Python.

The Code


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

# main window
root = Tk()
root.title("PhotoEditor")
root.geometry("640x450")

# color code
color = {
    "background": "#009973",
    "canvas": "#00b386",
}

# functions
def selected():
    global img_path, img
    img_path = filedialog.askopenfilename(initialdir=os.getcwd()) 
    img = Image.open(img_path)
    img.thumbnail((350, 350))
    img1 = ImageTk.PhotoImage(img)
    canvas2.create_image(300, 210, image=img1)
    canvas2.image=img1
    ui_controls()
    root.geometry("640x650")
    selectbtn.place(x=100, y=595)
    savebtn.place(x=280, y=595)
    exitbtn.place(x=460, y=595)


def blur(event):
    global img_path, img1, imgg
    for m in range(0, v1.get()+1):
            img = Image.open(img_path)
            img.thumbnail((350, 350))
            imgg = img.filter(ImageFilter.BoxBlur(m))
            img1 = ImageTk.PhotoImage(imgg) 
            canvas2.create_image(300, 210, image=img1)
            canvas2.image=img1


def brightness(event):
    global img_path, img2, img3
    for m in range(0, v2.get()+1):
            img = Image.open(img_path)
            img.thumbnail((350, 350))
            imgg = ImageEnhance.Brightness(img)
            img2 = imgg.enhance(m)
            img3 = ImageTk.PhotoImage(img2)
            canvas2.create_image(300, 210, image=img3)
            canvas2.image=img3


def contrast(event):
    global img_path, img4, img5
    for m in range(0, v3.get()+1):
            img = Image.open(img_path)
            img.thumbnail((350, 350))
            imgg = ImageEnhance.Contrast(img)
            img4 = imgg.enhance(m)
            img5 = ImageTk.PhotoImage(img4)
            canvas2.create_image(300, 210, image=img5)
            canvas2.image=img5

# All ui
select = False
img1 = None
canvas2 = Canvas(root, width="600", height="420", relief=RIDGE, bg=color["canvas"], bd=0)
canvas2.config(highlightbackground=color["background"])
canvas2.place(x=15, y=15)

v1 = IntVar()
v2 = IntVar()
v3 = IntVar()

style = ttk.Style()
style.configure("TScale", background=color["background"])

def ui_controls():
    blurr = Label(root, text="Blur:", font=("ariel 17 bold"), width=9, anchor='e')
    blurr.configure(bg=color["background"])
    blurr.place(x=170, y=455)
    blur_scale = ttk.Scale(root, from_=0, to=10, variable=v1, orient=HORIZONTAL, style="TScale", command=blur)
    blur_scale.place(x=305, y=460)

    bright = Label(root, text="Brightness:", font=("ariel 17 bold"))
    bright.configure(bg=color["background"])
    bright.place(x=163, y=495)
    bright_scale = ttk.Scale(root, from_=0, to=10, variable=v2, orient=HORIZONTAL, command=brightness)
    bright_scale.place(x=305, y=500)

    contrast = Label(root, text="Contrast:", font=("ariel 17 bold"))
    contrast.configure(bg=color["background"])
    contrast.place(x=190, y=535)
    contrast_scale = ttk.Scale(root, from_=0, to=10, variable=v3, orient=HORIZONTAL, command=contrast) 
    contrast_scale.place(x=305, y=540)

selectbtn = Button(root, text="Select Image", bg="royalblue", fg="white", font=("ariel 15 bold"), relief=GROOVE, command=selected)
selectbtn.place(x=100, y=395)

savebtn = Button(root, text="Save", width=12, bg='royalblue', fg='white', font=('ariel 15 bold'), relief=GROOVE)
savebtn.place(x=280, y=395)

exitbtn = Button(root, text="Exit", width=12, bg='red', fg='white', font=('ariel 15 bold'), relief=GROOVE, command=root.destroy)
exitbtn.place(x=460, y=395)


# menu bar
my_menu = Menu(root)
root.config(menu=my_menu)
root.configure(bg=color["background"])

#file menu
file_menu = Menu(my_menu, tearoff=False)
file_menu.add_command(label="New")
file_menu.add_command(label="Save as")
file_menu.add_command(label="Save")
file_menu.add_command(label="Open", command=selected)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
my_menu.add_cascade(label="File", menu=file_menu)

# __main__
root.mainloop()


This is how the software looks...



0 Comments