Skip to content Skip to sidebar Skip to footer

Tkinter, Overlay Foreground Image On Top Of A Background Image With Transparency

I have 2 images, in png format. The second image, is a shape with transparent background. First image: Second image: I cannot make the second image on top of the first at given c

Solution 1:

Thanks Bryan. Got it, in addition to label, the issue of transparency resolved from another question here (same foregound image used as a mask)

I guess, this is what cost me -2 points :-|

Now it works as expected.

from Tkinter import *
import Tkinter
from PIL import Image, ImageTk

root = Tkinter.Tk()  # A root window for displaying objects# open image
imageHead = Image.open('head.png')
imageHand = Image.open('hand.png')

imageHead.paste(imageHand, (20, 40), imageHand)
# Convert the Image object into a TkPhoto object
tkimage = ImageTk.PhotoImage(imageHead)

panel1 = Label(root, image=tkimage)
panel1.grid(row=0, column=2, sticky=E)
root.mainloop()  # Start the GUI

Solution 2:

Hey guys I know that I am 6 years late but I can help you with this

from PIL import Image

import numpy as np

# Create Image
img = Image.open("forground.png")

background = Image.open("background.png")

background.paste(img, (0, 0), img)
background.save('NewImg.png',"PNG")

NewImg = Image.open('NewImg.png')

# Use Image
tkimage = ImageTk.PhotoImage(NewImg)

panel1 = Label(root, image=tkimage)
panel1.grid(row=0, column=2, sticky=E)
root.mainloop()  # Start the GUI

So all you have to do is use NewImg when dealing with the file. This code creates an image out of the two images and then utilises that image in the program.

Post a Comment for "Tkinter, Overlay Foreground Image On Top Of A Background Image With Transparency"