|
| 1 | +from tkinter import * |
| 2 | +from tkinter import messagebox |
| 3 | +from tkinter.filedialog import askopenfilename |
| 4 | +from PIL import Image as Im |
| 5 | + |
| 6 | +GREEN = "#9bdeac" |
| 7 | +YELLOW = "#f7f5dd" |
| 8 | +FONT_NAME = "Arial" |
| 9 | + |
| 10 | + |
| 11 | +def select_watermark(): |
| 12 | + # RETRIEVE PATH OF IMAGE |
| 13 | + filename = askopenfilename(filetypes=(("jpg file", "*.jpg"), ("png file", '*.png'))) |
| 14 | + image1 = Im.open(filename) |
| 15 | + # RE-SIZE |
| 16 | + size = (150, 150) |
| 17 | + image1.thumbnail(size) |
| 18 | + # SAVE |
| 19 | + image1.save("water_mark.png") |
| 20 | + messagebox.showinfo('DONE', 'The Selected imaged is ready to be inserted to your main image as a 150px by 150px ' |
| 21 | + 'watermark\n\n Select your main Image.') |
| 22 | + |
| 23 | + |
| 24 | +def add_watermark(): |
| 25 | + # RETRIEVE PATH OF IMAGE |
| 26 | + filename = askopenfilename(filetypes=(("jpg file", "*.jpg"), ("png file", '*.png'))) |
| 27 | + |
| 28 | + # OPEN SELECTED FILE WITH PILLOW |
| 29 | + base = Im.open(filename) |
| 30 | + |
| 31 | + # OPEN DESIRED WATERMARK (SMALL PICTURE FILE INSIDE SAME LEVEL DIRECTORY) |
| 32 | + watermark = Im.open('water_mark.png') |
| 33 | + |
| 34 | + # GET DIMENSIONS OF SELECTED FILE AND USE THEM TO POSITION WATERMARK |
| 35 | + width, height = base.size |
| 36 | + pos = (int(width * .85), int(height * .85)) |
| 37 | + |
| 38 | + # PASTE WATERMARK INTO IMAGE |
| 39 | + base.paste(watermark, pos) |
| 40 | + # SAVE WATERMARKED IMAGE AT SPECIFIED DIRECTORY |
| 41 | + output = 'images/' + 'watermarked.png' |
| 42 | + base.save(output) |
| 43 | + messagebox.showinfo('SUCCESS', 'Success!\nYour image has been watermarked and saved.\n\n ' |
| 44 | + 'You can find it in the images folder as: "watermarked.png" ') |
| 45 | + window.quit() |
| 46 | + |
| 47 | + |
| 48 | +# ---------------------------GUI------------------------------- |
| 49 | +window = Tk() |
| 50 | +window.title("Water Mark Inserter") |
| 51 | +window.config(padx=50, pady=50, bg='black') |
| 52 | + |
| 53 | +# ------------------------------ LABEL-------------------------- |
| 54 | +heading_label = Label(text='WATERMARK INSERTER', font=(FONT_NAME, 40, 'bold'), bg='BLACK', fg=GREEN) |
| 55 | +heading_label.grid(row=0, column=1, columnspan=3) |
| 56 | + |
| 57 | +label = Label(window, text="Add the the following watermark to the bottom corner of your images", fg=YELLOW, bg='black') |
| 58 | +label.grid(row=1, column=1, columnspan=3) |
| 59 | + |
| 60 | +# ----------------------------- BUTTONS-------------------------- |
| 61 | +b1 = Button(window, text="CHOOSE WATER MARK PICTURE FILE", font=40, command=select_watermark, highlightthickness=1) |
| 62 | +b1.grid(row=3, column=2, pady=10) |
| 63 | +b1 = Button(window, text="CHOOSE MAIN PICTURE", font=40, command=add_watermark, highlightthickness=1) |
| 64 | +b1.grid(row=4, column=2, pady=10) |
| 65 | + |
| 66 | +window.mainloop() |
0 commit comments