Create a simple Python Photo Editor app
Published February 07, 2022Editing photos is a vital activity that helps us generate visually attractive photos that are both professional and appealing. In this python example, we'll look at how to make a basic photo editing application in Python. This application allows users to upload and modify images of their choosing before saving them to their files. Users will be able to do the following using the app:
- Image blurring
- Image rotation
- Image brightness may be increased or decreased.
- Set up the picture contrast.
- Toggle/rotate the picture to the right or left.
- Make a border for the picture.
The easy steps below demonstrate how to build a photo editing application.
Step 1: Install the prerequisite libraries. We will use the Tkinter and Pillow (PIL) libraries to create this application. We will not have to install the Tkinter library since it is already a preinstalled Python library. The Pillow (PIL) library, an external library, has to be installed. To do so, open your terminal and execute the following command:
pip install Pillow |
![]() |
The PIL module has been successfully installed. The module will enable us to develop picture editing features such as adjusting the contrast, blurring an image, brightening an image, rotating an image, adding borders to an image, and flipping an image.
Step 2: We can now import all of the essential libraries into our application after installing the PIL module. Simply enter the following code to do this:
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 |
Step 3: Let's now create the application's UI. We'll need to make 3 buttons: one for picking a picture, saving the altered image, and exiting the application. In addition, we will design labels and user input forms, dropdown menus, and combo boxes for picture editing options such as rotate, flip, blur, brightness, contrast, and add a border.
To create the UI, simply paste the code below:
root = Tk() root.title("A Simple Python Photo Editor") root.geometry("640x640") blurr = Label(root, text="Blur:", font=("ariel 12 bold"), width=9, anchor='e') blurr.place(x=15, y=8) v1 = IntVar() scale1 = ttk.Scale(root, from_=0, to=10, variable=v1, orient=HORIZONTAL, command=blur) scale1.place(x=150, y=10) bright = Label(root, text="Brightness:", font=("ariel 12 bold")) bright.place(x=8, y=50) v2 = IntVar() scale2 = ttk.Scale(root, from_=0, to=10, variable=v2, orient=HORIZONTAL, command=brightness) scale2.place(x=150, y=55) contrast = Label(root, text="Contrast:", font=("ariel 12 bold")) contrast.place(x=35, y=92) v3 = IntVar() scale3 = ttk.Scale(root, from_=0, to=10, variable=v3, orient=HORIZONTAL, command=contrast) scale3.place(x=150, y=100) rotate = Label(root, text="Rotate:", font=("ariel 12 bold")) rotate.place(x=370, y=8) values = [0, 90, 180, 270, 360] rotate_combo = ttk.Combobox(root, values=values, font=('ariel 10 bold')) rotate_combo.place(x=460, y=15) rotate_combo.bind("<<ComboboxSelected>>", rotate_image) flip = Label(root, text="Flip:", font=("ariel 12 bold")) flip.place(x=400, y=50) values1 = ["FLIP LEFT TO RIGHT", "FLIP TOP TO BOTTOM"] flip_combo = ttk.Combobox(root, values=values1, font=('ariel 10 bold')) flip_combo.place(x=460, y=57) flip_combo.bind("<<ComboboxSelected>>", flip_image) border = Label(root, text="Add border:", font=("ariel 12 bold")) border.place(x=320, y=92) values2 = [i for i in range(10, 45, 5)] border_combo = ttk.Combobox(root, values=values2, font=("ariel 10 bold")) border_combo.place(x=460, y=99) border_combo.bind("<<ComboboxSelected>>", image_border) # create canvas to display image canvas2 = Canvas(root, width="500", height="320", relief=RIDGE, bd=2) canvas2.place(x=15, y=150) # create buttons btn1 = Button(root, text="Select Image", font=('ariel 12 bold'), relief=GROOVE, command=selected) btn1.place(x=100, y=595) btn2 = Button(root, text="Save", width=12, font=('ariel 12 bold'), relief=GROOVE, command=save) btn2.place(x=280, y=595) btn3 = Button(root, text="Close", width=12, font=('ariel 12 bold'), relief=GROOVE, command=root.destroy) btn3.place(x=460, y=595) root.mainloop() |
If you run the code above, the following will be displayed.
![]() |
Our user interface seems to be in excellent shape. However, it is unable to carry out our picture editing functions. The next step will create the functions that will allow them to execute their respective duties.
Step 4: Create functions for each of the functionality.
Create a function that adds blur to a picture.
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 |
Create an image rotation function.
def rotate_image(event): global img_path, img6, img7 img = Image.open(img_path) img.thumbnail((350, 350)) img6 = img.rotate(int(rotate_combo.get())) img7 = ImageTk.PhotoImage(img6) canvas2.create_image(300, 210, image=img7) canvas2.image = img7 |
function for increasing the brightness of a picture
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 |
Make a function that flips a picture.
def flip_image(event): global img_path, img8, img9 img = Image.open(img_path) img.thumbnail((350, 350)) if flip_combo.get() == "FLIP LEFT TO RIGHT": img8 = img.transpose(Image.FLIP_LEFT_RIGHT) elif flip_combo.get() == "FLIP TOP TO BOTTOM": img8 = img.transpose(Image.FLIP_TOP_BOTTOM) img9 = ImageTk.PhotoImage(img8) canvas2.create_image(300, 210, image=img9) canvas2.image = img9 |
function for enhancing picture contrast
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 |
Create a border-adding function.
def image_border(event): global img_path, img10, img11 img = Image.open(img_path) img.thumbnail((350, 350)) img10 = ImageOps.expand(img, border=int(border_combo.get()), fill=95) img11 = ImageTk.PhotoImage(img10) canvas2.create_image(300, 210, image=img11) canvas2.image = img11 |
Create an image selection function.
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 |
To save a modified picture, utilize the create function.
def save(): global img_path, imgg, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11 # file=None ext = img_path.split(".")[-1] file = asksaveasfilename(defaultextension=f".{ext}", filetypes=[("All Files", "*.*"), ("PNG file", "*.png"), ("jpg file", "*.jpg")]) if file: if canvas2.image == img1: imgg.save(file) elif canvas2.image == img3: img2.save(file) elif canvas2.image == img5: img4.save(file) elif canvas2.image == img7: img6.save(file) elif canvas2.image == img9: img8.save(file) elif canvas2.image == img11: img10.save(file) |
Step 5: We have completed the development of our picture editing application. Run the program now. Every feature works as intended.
Output Screenshots
![]() |
![]() |
Conclusion: In this python example we created simple image editor app with tkinter
Article Contributed By :
|
|
|
|
343 Views |