So I have 2 images that I would like to display on top of each other.
The image on top should have transparency so that the one on the bottom is visible.
Here is my code so far:
from Tkinter import *
import ttk
from PIL import Image, ImageTk
root = Tk()
face = Image.open("faces/face.gif")
eyes = Image.open("faces/eyes1.png")
face = face.convert("RGBA")
eyes = eyes.convert("RGBA")
facedatas = face.getdata()
eyesdatas = eyes.getdata()
newData = []
for item in eyesdatas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
eyes.putdata(newData)
eyes.save("eyes0.png", "PNG")
facepic = ImageTk.PhotoImage(face)
eyespic = ImageTk.PhotoImage(eyes)
label1 = Label(image=facepic)
label1.image = facepic
label1.grid(row = 0, column = 0)
label2 = Label(image=eyespic)
label2.image = eyespic
label2.grid(row = 0, column = 0)
root.mainloop()
And here is what I get when I run it:
When I would like to obtain this:
With the face in the bottom and the eyes on top.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…