Reputation: 1206
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 coordinates (x,y) with the first image visible through the transparent zone of the second image.
Result desired:
import Tkinter
import Image, ImageTk
# open an image
head = Image.open('background2.png')
hand = Image.open('foreground2.png')
root = Tkinter.Tk() # A root window for displaying objects
head.paste(hand,(20,20))
# Convert the Image object into a TkPhoto object
tkimage = ImageTk.PhotoImage(head)
root.mainloop() # Start the GUI
An empty tk window is displayed.
Upvotes: 7
Views: 18032
Reputation: 77
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.
Upvotes: 1
Reputation: 1206
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
Upvotes: 4