Friday, December 4, 2020

Blend images with PIL in Python


# https://www.youtube.com/watch?v=7IdBppdM23Y&list=PL1H1sBF1VAKXCayO4KZqSmym2Z_sn6Pha&index=3

# Before loading files, ensure images to be blended have same dimensions


from tkinter import *

from PIL import ImageTk, Image


win = Tk()

win.title("Blending images")

win.geometry("720x700")


file1 = "..\\Images\\Amazon.png"

file2 = "..\\Images\\Elephant.png"


def Save(e):

  img1 = Image.open(file1)

  img2 = Image.open(file2)

  imgBlend = Image.blend(img1, img2, D.get()/100)

  imgBlend.save('..\\Images\\Blended.png')

  pic = PhotoImage(file='..\\Images\\Blended.png')

  lblBlend.config(image=pic)

  lblBlend.image = pic

  del img1, img2

  

D = DoubleVar()

scl1 = Scale(win, variable=D, from_=0, to=100, orient=HORIZONTAL, length=600, sliderlength=10)

scl1.bind("<Motion>", Save)

scl1.place(x=30, y=10)


openImage1 = Image.open("..\\Images\\Amazon.png")

openImage1.thumbnail((300, 300))

img1 = ImageTk.PhotoImage(openImage1)

lbl1 = Label(win, image=img1)

lbl1.place(x=30, y=60)


openImage2 = Image.open("..\\Images\\Elephant.png")

openImage2.thumbnail((300, 300))

img2 = ImageTk.PhotoImage(openImage2)

lbl2 = Label(win, image=img2)

lbl2.place(x=375, y=60)


openImage2 = Image.open("..\\Images\\Amazon.png")

imgStart = ImageTk.PhotoImage(openImage2)

lblBlend = Label(win, image=imgStart)

lblBlend.place(x=75, y=260)


No comments:

Post a Comment