Python - 如何在窗口中显示透明图像?

问题描述

我正在尝试将已经具有透明背景的图像显示到窗口中。目前我正在使用 OpenCV cv2.imshow,它不显示 alpha 通道,导致像素为黑色。是否有任何其他库或不同类型的方法可以在显示背景桌面屏幕的窗口中显示具有透明背景的图像?

原始图片

enter image description here

当前结果:

想要的结果:

enter image description here

解决方法

您可以使用 python 标准库 Tkinter 在透明窗口中显示图像。

代码片段:

from tkinter import Tk,Canvas,PhotoImage,NW

root = Tk()

root.attributes('-transparentcolor','#f0f0f0')

# Canvas
canvas = Canvas(root,width=450,height=600)
canvas.pack()

# Image
img = PhotoImage(file="./images/panda.png")

# Positioning the Image inside the canvas
canvas.create_image(0,anchor=NW,image=img)

# Starts the GUI
root.mainloop()