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

2024-09-12 07:53:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在窗口中显示一个已经有透明背景的图像。目前我正在使用OpenCVcv2.imshow,它不显示alpha通道,这会导致像素变黑。是否有其他库或不同类型的方法可以在后台桌面屏幕显示的窗口中显示透明背景的图像

原始图像:

enter image description here

当前结果:

预期结果:

enter image description here


Tags: 方法图像alpha类型像素桌面后台背景
1条回答
网友
1楼 · 发布于 2024-09-12 07:53:03

您可以使用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, 0, anchor=NW, image=img)

# Starts the GUI
root.mainloop()

相关问题 更多 >