|
| 1 | +from tkinter import * |
| 2 | +from tkinter import ttk |
| 3 | +from tkinter import filedialog |
| 4 | +from tkinter.filedialog import askopenfilename, asksaveasfilename |
| 5 | +from PIL import Image, ImageTk, ImageFilter, ImageEnhance, ImageOps |
| 6 | +import os |
| 7 | + |
| 8 | +# Create a Tkinter window |
| 9 | +root = Tk() # Create a window |
| 10 | +root.title("Photo Editor App") # Set the title of the window |
| 11 | +root.geometry("640x640") # Set the size of the window |
| 12 | + |
| 13 | + |
| 14 | +def select(): # Load images from the computer |
| 15 | + global img_path, img |
| 16 | + img_path = filedialog.askopenfilename(initialdir=os.getcwd()) |
| 17 | + img = Image.open(img_path) |
| 18 | + img.thumbnail((350, 350)) |
| 19 | + img1 = ImageTk.PhotoImage(img) |
| 20 | + canvas2.create_image(300, 210, image=img1) |
| 21 | + canvas2.image = img1 |
| 22 | + |
| 23 | + |
| 24 | +def blur(event): # The Blur effect |
| 25 | + global img_path, img1, imgg |
| 26 | + for m in range(0, v1.get()+1): |
| 27 | + img = Image.open(img_path) |
| 28 | + img.thumbnail((350, 350)) |
| 29 | + imgg = img.filter(ImageFilter.BoxBlur(m)) |
| 30 | + img1 = ImageTk.PhotoImage(imgg) |
| 31 | + canvas2.create_image(300, 210, image=img1) |
| 32 | + canvas2.image = img1 |
| 33 | + |
| 34 | + |
| 35 | +def brightness(event): # The brightness effect |
| 36 | + global img_path, img2, img3 |
| 37 | + for m in range(0, v2.get()+1): |
| 38 | + img = Image.open(img_path) |
| 39 | + img.thumbnail((350, 350)) |
| 40 | + imgg = ImageEnhance.Brightness(img) |
| 41 | + img2 = imgg.enhance(m) |
| 42 | + img3 = ImageTk.PhotoImage(img2) |
| 43 | + canvas2.create_image(300, 210, image=img3) |
| 44 | + canvas2.image = img3 |
| 45 | + |
| 46 | + |
| 47 | +def contrast(event): |
| 48 | + global img_path, img4, img5 |
| 49 | + for m in range(0, v3.get()+1): |
| 50 | + img = Image.open(img_path) |
| 51 | + img.thumbnail((350, 350)) |
| 52 | + imgg = ImageEnhance.Contrast(img) |
| 53 | + img4 = imgg.enhance(m) |
| 54 | + img5 = ImageTk.PhotoImage(img4) |
| 55 | + canvas2.create_image(300, 210, image=img5) |
| 56 | + canvas2.image = img5 |
| 57 | + |
| 58 | + |
| 59 | +def rotate(event): |
| 60 | + global img_path, img6, img7 |
| 61 | + img = Image.open(img_path) |
| 62 | + img.thumbnail((350, 350)) |
| 63 | + img6 = img.rotate(int(rotate_combo.get())) |
| 64 | + img7 = ImageTk.PhotoImage(img6) |
| 65 | + canvas2.create_image(300, 210, image=img7) |
| 66 | + canvas2.image = img7 |
| 67 | + |
| 68 | + |
| 69 | +def flip(event): |
| 70 | + global img_path, img8, img9 |
| 71 | + img = Image.open(img_path) |
| 72 | + img.thumbnail((350, 350)) |
| 73 | + if flip_combo.get() == "FLIP LEFT TO RIGHT": |
| 74 | + img8 = img.transpose(Image.FLIP_LEFT_RIGHT) |
| 75 | + elif flip_combo.get() == "FLIP TOP TO BOTTOM": |
| 76 | + img8 = img.transpose(Image.FLIP_TOP_BOTTOM) |
| 77 | + img9 = ImageTk.PhotoImage(img8) |
| 78 | + canvas2.create_image(300, 210, image=img9) |
| 79 | + canvas2.image = img9 |
| 80 | + |
| 81 | + |
| 82 | +def border(event): |
| 83 | + global img_path, img10, img11 |
| 84 | + img = Image.open(img_path) |
| 85 | + img.thumbnail((350, 350)) |
| 86 | + img10 = ImageOps.expand(img, border=int(border_combo.get()), fill=95) |
| 87 | + img11 = ImageTk.PhotoImage(img10) |
| 88 | + canvas2.create_image(300, 210, image=img11) |
| 89 | + canvas2.image = img11 |
| 90 | + |
| 91 | + |
| 92 | +img1 = None |
| 93 | +img3 = None |
| 94 | +img5 = None |
| 95 | +img7 = None |
| 96 | +img9 = None |
| 97 | +img11 = None |
| 98 | + |
| 99 | + |
| 100 | +def save(): |
| 101 | + global img_path, imgg, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11 |
| 102 | + ext = img_path.split(".")[-1] |
| 103 | + file = asksaveasfilename(defaultextension=f".{ext}", filetypes=[( |
| 104 | + "All Files", "*.*"), ("PNG file", "*.png"), ("jpg file", "*.jpg")]) |
| 105 | + if file: |
| 106 | + if canvas2.image == img1: |
| 107 | + imgg.save(file) |
| 108 | + elif canvas2.image == img3: |
| 109 | + img2.save(file) |
| 110 | + elif canvas2.image == img5: |
| 111 | + img4.save(file) |
| 112 | + elif canvas2.image == img7: |
| 113 | + img6.save(file) |
| 114 | + elif canvas2.image == img9: |
| 115 | + img8.save(file) |
| 116 | + elif canvas2.image == img11: |
| 117 | + img10.save(file) |
| 118 | + |
| 119 | + |
| 120 | +blurr = Label(root, text="Blur:", font=("ariel 17 bold"), width=9, anchor='e') |
| 121 | +blurr.place(x=15, y=8) |
| 122 | +v1 = IntVar() |
| 123 | +scale1 = ttk.Scale(root, from_=0, to=10, variable=v1, |
| 124 | + orient=HORIZONTAL, command=blur) |
| 125 | +scale1.place(x=150, y=10) |
| 126 | +bright = Label(root, text="Brightness:", font=("ariel 17 bold")) |
| 127 | +bright.place(x=8, y=50) |
| 128 | +v2 = IntVar() |
| 129 | +scale2 = ttk.Scale(root, from_=0, to=10, variable=v2, |
| 130 | + orient=HORIZONTAL, command=brightness) |
| 131 | +scale2.place(x=150, y=55) |
| 132 | +contrast = Label(root, text="Contrast:", font=("ariel 17 bold")) |
| 133 | +contrast.place(x=35, y=92) |
| 134 | +v3 = IntVar() |
| 135 | +scale3 = ttk.Scale(root, from_=0, to=10, variable=v3, |
| 136 | + orient=HORIZONTAL, command=contrast) |
| 137 | +scale3.place(x=150, y=100) |
| 138 | +rotate = Label(root, text="Rotate:", font=("ariel 17 bold")) |
| 139 | +rotate.place(x=370, y=8) |
| 140 | +values = [0, 90, 180, 270, 360] |
| 141 | +rotate_combo = ttk.Combobox(root, values=values, font=('ariel 10 bold')) |
| 142 | +rotate_combo.place(x=460, y=15) |
| 143 | +rotate_combo.bind("<<ComboboxSelected>>", rotate) |
| 144 | +flip = Label(root, text="Flip:", font=("ariel 17 bold")) |
| 145 | +flip.place(x=400, y=50) |
| 146 | +values1 = ["FLIP LEFT TO RIGHT", "FLIP TOP TO BOTTOM"] |
| 147 | +flip_combo = ttk.Combobox(root, values=values1, font=('ariel 10 bold')) |
| 148 | +flip_combo.place(x=460, y=57) |
| 149 | +flip_combo.bind("<<ComboboxSelected>>", flip) |
| 150 | +border = Label(root, text="Add border:", font=("ariel 17 bold")) |
| 151 | +border.place(x=320, y=92) |
| 152 | +values2 = [i for i in range(10, 45, 5)] |
| 153 | +border_combo = ttk.Combobox(root, values=values2, font=("ariel 10 bold")) |
| 154 | +border_combo.place(x=460, y=99) |
| 155 | +border_combo.bind("<<ComboboxSelected>>", border) |
| 156 | + |
| 157 | +# create canvas to display image |
| 158 | +canvas2 = Canvas(root, width="600", height="420", relief=RIDGE, bd=2) |
| 159 | +canvas2.place(x=15, y=150) |
| 160 | + |
| 161 | +# create buttons |
| 162 | +btn1 = Button(root, text="Select Image", bg='black', fg='gold', |
| 163 | + font=('ariel 15 bold'), relief=GROOVE, command=select) |
| 164 | +btn1.place(x=100, y=595) |
| 165 | +btn2 = Button(root, text="Save", width=12, bg='black', fg='gold', |
| 166 | + font=('ariel 15 bold'), relief=GROOVE, command=save) |
| 167 | +btn2.place(x=280, y=595) |
| 168 | +btn3 = Button(root, text="Exit", width=12, bg='black', fg='gold', |
| 169 | + font=('ariel 15 bold'), relief=GROOVE, command=root.destroy) |
| 170 | +btn3.place(x=460, y=595) |
| 171 | + |
| 172 | +# Execute Tkinter |
| 173 | +root.mainloop() |
0 commit comments