|
| 1 | +# import required modules |
| 2 | + |
| 3 | +from tkinter import * |
| 4 | +from tkinter import ttk |
| 5 | +from tkinter import filedialog |
| 6 | +from tkinter.filedialog import askopenfilename,asksaveasfilename |
| 7 | +from PIL import Image, ImageTk, ImageFilter, ImageEnhance, ImageOps |
| 8 | +import os |
| 9 | + |
| 10 | +#------------------------------------------------------------------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +# contrast border thumbnail |
| 13 | + |
| 14 | +root = Tk() |
| 15 | +root.title("Rammya's Image Editor") # name for the window |
| 16 | +root.geometry("700x640") # set size of window |
| 17 | +root.configure(bg='magenta') # background color |
| 18 | + |
| 19 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 20 | + |
| 21 | +# create functions |
| 22 | + |
| 23 | +def selected(): # function to select image |
| 24 | + global img_path, img |
| 25 | + img_path = filedialog.askopenfilename(initialdir=os.getcwd()) # path file |
| 26 | + img = Image.open(img_path) # open the file |
| 27 | + img.thumbnail((350, 350)) |
| 28 | + #imgg = img.filter(ImageFilter.BoxBlur(0)) |
| 29 | + img1 = ImageTk.PhotoImage(img) |
| 30 | + canvas2.create_image(300, 210, image=img1) # set image in canvas |
| 31 | + canvas2.image=img1 |
| 32 | + |
| 33 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 34 | + |
| 35 | +def blur(event): # function to blur the image |
| 36 | + global img_path, img1, imgg |
| 37 | + for m in range(0, v1.get()+1): |
| 38 | + img = Image.open(img_path) |
| 39 | + img.thumbnail((350, 350)) |
| 40 | + imgg = img.filter(ImageFilter.BoxBlur(m)) |
| 41 | + img1 = ImageTk.PhotoImage(imgg) |
| 42 | + canvas2.create_image(300, 210, image=img1) |
| 43 | + canvas2.image=img1 |
| 44 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 45 | + |
| 46 | +def brightness(event): # function to brightness the image |
| 47 | + global img_path, img2, img3 |
| 48 | + for m in range(0, v2.get()+1): |
| 49 | + img = Image.open(img_path) |
| 50 | + img.thumbnail((350, 350)) |
| 51 | + imgg = ImageEnhance.Brightness(img) |
| 52 | + img2 = imgg.enhance(m) |
| 53 | + img3 = ImageTk.PhotoImage(img2) |
| 54 | + canvas2.create_image(300, 210, image=img3) |
| 55 | + canvas2.image=img3 |
| 56 | + |
| 57 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 58 | + |
| 59 | +def contrast(event): # function to contrast the image |
| 60 | + global img_path, img4, img5 |
| 61 | + for m in range(0, v3.get()+1): |
| 62 | + img = Image.open(img_path) |
| 63 | + img.thumbnail((350, 350)) |
| 64 | + imgg = ImageEnhance.Contrast(img) |
| 65 | + img4 = imgg.enhance(m) |
| 66 | + img5 = ImageTk.PhotoImage(img4) |
| 67 | + canvas2.create_image(300, 210, image=img5) |
| 68 | + canvas2.image=img5 |
| 69 | + |
| 70 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 71 | + |
| 72 | +def rotate_image(event): # function to rotate the image |
| 73 | + global img_path, img6, img7 |
| 74 | + img = Image.open(img_path) |
| 75 | + img.thumbnail((350, 350)) |
| 76 | + img6 = img.rotate(int(rotate_combo.get())) |
| 77 | + img7 = ImageTk.PhotoImage(img6) |
| 78 | + canvas2.create_image(300, 210, image=img7) |
| 79 | + canvas2.image=img7 |
| 80 | + |
| 81 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 82 | + |
| 83 | +def flip_image(event): # function to flip flop the image |
| 84 | + global img_path, img8, img9 |
| 85 | + img = Image.open(img_path) |
| 86 | + img.thumbnail((350, 350)) |
| 87 | + if flip_combo.get() == "FLIP LEFT TO RIGHT": |
| 88 | + img8 = img.transpose(Image.FLIP_LEFT_RIGHT) |
| 89 | + elif flip_combo.get() == "FLIP TOP TO BOTTOM": |
| 90 | + img8 = img.transpose(Image.FLIP_TOP_BOTTOM) |
| 91 | + img9 = ImageTk.PhotoImage(img8) |
| 92 | + canvas2.create_image(300, 210, image=img9) |
| 93 | + canvas2.image=img9 |
| 94 | + |
| 95 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 96 | + |
| 97 | +def image_border(event): # function to add border the image |
| 98 | + global img_path, img10, img11 |
| 99 | + img = Image.open(img_path) |
| 100 | + img.thumbnail((350, 350)) |
| 101 | + img10 = ImageOps.expand(img, border=int(border_combo.get()), fill=95) |
| 102 | + img11 = ImageTk.PhotoImage(img10) |
| 103 | + canvas2.create_image(300, 210, image=img11) |
| 104 | + canvas2.image=img11 |
| 105 | +img1 = None |
| 106 | +img3 = None |
| 107 | +img5 = None |
| 108 | +img7 = None |
| 109 | +img9 = None |
| 110 | +img11 = None |
| 111 | + |
| 112 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 113 | + |
| 114 | +def save(): # save the file |
| 115 | + global img_path, imgg, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11 |
| 116 | + #file=None |
| 117 | + ext = img_path.split(".")[-1] |
| 118 | + file=asksaveasfilename(defaultextension =f".{ext}",filetypes=[("All Files","*.*"),("PNG file","*.png"),("jpg file","*.jpg")]) |
| 119 | + if file: # choose the image file to save |
| 120 | + if canvas2.image==img1: |
| 121 | + imgg.save(file) |
| 122 | + elif canvas2.image==img3: |
| 123 | + img2.save(file) |
| 124 | + elif canvas2.image==img5: |
| 125 | + img4.save(file) |
| 126 | + elif canvas2.image==img7: |
| 127 | + img6.save(file) |
| 128 | + elif canvas2.image==img9: |
| 129 | + img8.save(file) |
| 130 | + elif canvas2.image==img11: |
| 131 | + img10.save(file) |
| 132 | + |
| 133 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 134 | + |
| 135 | +# create labels, scales and comboboxes |
| 136 | + |
| 137 | +blu = Label(root, text="BLUR ", font=("arial 15 bold"), width=9, anchor='e', bg = 'magenta') |
| 138 | +blu.place(x=15, y=8) |
| 139 | +v1 = IntVar() |
| 140 | +scale1 = ttk.Scale(root, from_=0, to=10, variable=v1, orient=HORIZONTAL, command=blur) # set the scale for blur |
| 141 | +scale1.place(x=150, y=10) |
| 142 | + |
| 143 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 144 | + |
| 145 | + |
| 146 | +bright = Label(root, text="BRIGHTNESS ", font=("arial 15 bold"), bg = 'magenta') |
| 147 | +bright.place(x=8, y=50) |
| 148 | +v2 = IntVar() |
| 149 | +scale2 = ttk.Scale(root, from_=0, to=10, variable=v2, orient=HORIZONTAL, command=brightness) # set the scale for brightness |
| 150 | +scale2.place(x=150, y=55) |
| 151 | + |
| 152 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 153 | + |
| 154 | +contrast = Label(root, text="CONTRAST ", font=("arial 15 bold"), bg = 'magenta') |
| 155 | +contrast.place(x=35, y=92) |
| 156 | +v3 = IntVar() |
| 157 | +scale3 = ttk.Scale(root, from_=0, to=10, variable=v3, orient=HORIZONTAL, command=contrast) # set the scale for contrast |
| 158 | +scale3.place(x=150, y=100) |
| 159 | + |
| 160 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 161 | + |
| 162 | +rotate = Label(root, text="ROTATE ", font=("arial 15 bold"), bg = 'magenta') |
| 163 | +rotate.place(x=370, y=8) |
| 164 | +values = [0, 90, 180, 270, 360] # values for rotate the image by angle |
| 165 | +rotate_combo = ttk.Combobox(root, values=values, font=('verdana 10 bold')) |
| 166 | +rotate_combo.place(x=460, y=15) |
| 167 | +rotate_combo.bind("<<ComboboxSelected>>", rotate_image) |
| 168 | + |
| 169 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 170 | + |
| 171 | +flip = Label(root, text="FLIP ", font=("arial 15 bold"), bg = 'magenta') |
| 172 | +flip.place(x=400, y=50) |
| 173 | +values1 = ["FLIP LEFT TO RIGHT", "FLIP TOP TO BOTTOM"] # choosing option for the flip |
| 174 | +flip_combo = ttk.Combobox(root, values=values1, font=('verdana 10 bold')) |
| 175 | +flip_combo.place(x=460, y=57) |
| 176 | +flip_combo.bind("<<ComboboxSelected>>", flip_image) |
| 177 | + |
| 178 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 179 | + |
| 180 | +border = Label(root, text="ADD BORDER ", font=("arial 15 bold"), bg = 'magenta') |
| 181 | +border.place(x=320, y=92) |
| 182 | +values2 = [i for i in range(10, 45, 5)] # adding border width to choose |
| 183 | +border_combo = ttk.Combobox(root, values=values2, font=("verdana 10 bold")) |
| 184 | +border_combo.place(x=460, y=99) |
| 185 | +border_combo.bind("<<ComboboxSelected>>", image_border) |
| 186 | + |
| 187 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 188 | + |
| 189 | +# create canvas to display image |
| 190 | +canvas2 = Canvas(root, width="600", height="420", relief=RIDGE, bd=2) |
| 191 | +canvas2.place(x=15, y=150) |
| 192 | + |
| 193 | +#------------------------------------------------------------------------------------------------------------------------------------------- |
| 194 | + |
| 195 | +# create buttons |
| 196 | +btn1 = Button(root, text="SELECT IMAGE", bg='black', fg='white', font=('verdana 10 bold'), relief=GROOVE, command=selected) # button for select image |
| 197 | +btn1.place(x=100, y=595) |
| 198 | +btn2 = Button(root, text="SAVE", width=12, bg='black', fg='white', font=('verdana 10 bold'), relief=GROOVE, command=save) # button for save |
| 199 | +btn2.place(x=280, y=595) |
| 200 | +btn3 = Button(root, text="EXIT", width=12, bg='black', fg='white', font=('verdana 10 bold'), relief=GROOVE, command=root.destroy) # button for exit |
| 201 | +btn3.place(x=460, y=595) |
| 202 | +root.mainloop() |
0 commit comments