Tkinter Image transparency

前端 未结 2 1288
故里飘歌
故里飘歌 2020-12-04 00:33

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

相关标签:
2条回答
  • 2020-12-04 00:57

    Try this

    from PIL import Image, ImageTk
    from Tkinter import Tk, Label
    
    root = Tk()
    
    def RBGAImage(path):
        return Image.open(path).convert("RGBA")
    
    face = RBGAImage("faces/face.gif")
    eyes = RBGAImage("faces/eyes1.png")
    
    face.paste(eyes, (0, 0), eyes)
    
    facepic = ImageTk.PhotoImage(face)
    
    label1 = Label(image=facepic)
    label1.grid(row = 0, column = 0)
    
    root.mainloop()
    

    I do not have both your source images, so I can not be sure it will work with them. Please provide the originals of both if there is any issue.

    0 讨论(0)
  • 2020-12-04 01:15

    You can use Image.alpha_composite to create a new composited image.

    from PIL import Image, ImageTk
    from Tkinter import Tk, Label
    
    root = Tk()
    
    def RBGAImage(path):
        return Image.open(path).convert("RGBA")
    
    face = RBGAImage("faces/face.gif")
    eyes = RBGAImage("faces/eyes1.png")
    
    c = Image.alpha_composite(face, eyes)
    
    facepic = ImageTk.PhotoImage(c)
    
    label1 = Label(image=facepic)
    label1.grid(row = 0, column = 0)
    
    root.mainloop()
    
    0 讨论(0)
提交回复
热议问题