python - Tkinter 图像透明度

标签 python tkinter python-imaging-library

所以我有 2 张图像,我想将它们显示在彼此之上。 顶部的图像应该具有透明度,以便底部的图像可见。

到目前为止,这是我的代码:

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()

这是我运行它时得到的结果:

Eyes

当我想获得这个:

face and eyes

脸在下,眼睛在上。

最佳答案

试试这个

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()

我没有你的源图像,所以我不能确定它是否适用于它们。如有任何问题,请提供两者的原件。

关于python - Tkinter 图像透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33904942/

相关文章:

python - 使用 tkinter 创建计时器

python - 无法将图像保存到 django 模型

python - 将图像转换为 numpy 数组需要什么?

python - Cython 用户的 Numpy 类型

Python:re 模块替换字典中的值

python - 选择GUI时如何去除黑边?

python - 类型错误 : __array__() takes 1 positional argument but 2 were given (Image classification Keras)

Python数组算法

python - @ 之前的 django EmailField 点

python - 无法在 MacOS 上使用 pyenv Python 安装 tkinter