|
| 1 | +import cv2 |
| 2 | +import tkinter as tk |
| 3 | +from tkinter.filedialog import * |
| 4 | + |
| 5 | +window = tk.Tk() |
| 6 | +window.title("Image blending") |
| 7 | +window.geometry('300x140') |
| 8 | + |
| 9 | +def image1(): # getting image 1 |
| 10 | + photo1 = askopenfilename() |
| 11 | + global img1 |
| 12 | + img1 = cv2.imread(photo1) |
| 13 | + img1 = cv2.resize(img1,(500,500)) |
| 14 | + |
| 15 | +def image2(): # getting image 2 |
| 16 | + photo2 = askopenfilename() |
| 17 | + global img2 |
| 18 | + img2 = cv2.imread(photo2) |
| 19 | + img2 = cv2.resize(img2,(500,500)) |
| 20 | + |
| 21 | +def proceeds(): # reading alpha and displaying output |
| 22 | + global alpha |
| 23 | + alpha = t.get(1.0, "end-1c") |
| 24 | + alpha = float(alpha) |
| 25 | + if alpha>=0 and alpha<=1: |
| 26 | + beta = 1-alpha |
| 27 | + res = cv2.addWeighted(img1, alpha, img2, beta, 0.0) |
| 28 | + cv2.imshow('Result', res) |
| 29 | + cv2.imwrite("Output.jpg", res) |
| 30 | + cv2.waitKey(0) |
| 31 | + else: # when alpha is invalid |
| 32 | + print("invalid alpha") |
| 33 | + exit() |
| 34 | + |
| 35 | +label = tk.Label(window, text="Enter alpha (0 to 1)").grid(row=0, column=0) |
| 36 | +label = tk.Label(window, text="Image 1").grid(row=1, column=0) |
| 37 | +label = tk.Label(window, text="Image 2").grid(row=2, column=0) |
| 38 | + |
| 39 | +t = tk.Text(window, height=1, width=5) |
| 40 | +b1 = tk.Button(window, text='choose image 1', command=image1) |
| 41 | +b2 = tk.Button(window, text='choose image 2', command=image2) |
| 42 | +proceed = tk.Button(window, text='Proceed', command=proceeds) |
| 43 | + |
| 44 | +t.grid(row=0,column=1) |
| 45 | +b1.grid(row=1,column=1) |
| 46 | +b2.grid(row=2,column=1) |
| 47 | +proceed.grid(row=3,column=1) |
| 48 | + |
| 49 | +window.mainloop() |
| 50 | +cv2.destroyAllWindows() |
0 commit comments