Tkinter,覆盖在背景图象顶部的前景图象与透明度 [英] Tkinter, overlay foreground image on top of a background image with transparency

查看:224
本文介绍了Tkinter,覆盖在背景图象顶部的前景图象与透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张图片,格式为png。
第二张图片是具有透明背景的形状。

I have 2 images, in png format. The second image, is a shape with transparent background.

第一张图片:

第二张图片:

我不能在给定坐标(x,y)的第一个图像的顶部制作第二个图像,第一个图像通过透明区域可见第二张图片。

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.

所需结果:

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

显示一个空的tk窗口。

An empty tk window is displayed.

推荐答案

感谢Bryan。
得到它,除了标签,透明度问题从另一个问题解决这里(用作面具的同一张图像)

Thanks Bryan. Got it, in addition to label, the issue of transparency resolved from another question here (same foregound image used as a mask)

我想,这就是我的成本 - 2分: - |

I guess, this is what cost me -2 points :-|

现在按预期工作。

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

这篇关于Tkinter,覆盖在背景图象顶部的前景图象与透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆