Tkinter图像透明度 [英] Tkinter Image transparency
本文介绍了Tkinter图像透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我要显示2张图像. 顶部的图像应具有透明度,以便底部的图像可见.
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:
当我想获得此信息时:
面部在底部,眼睛在顶部.
With the face in the bottom and the eyes on top.
推荐答案
尝试一下
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.
这篇关于Tkinter图像透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文