简体   繁体   中英

Python - How to show a transparent image in a window?

I'm trying to display an image that already has a transparent background into a window. Currently I'm using OpenCV cv2.imshow which doesn't show the alpha channel and that results in the pixels being black. Are there any other library or different kinds of approach that shows an image with a transparent background in a window with the background desktop screen showing?

Original Image:

在此处输入图像描述

Current result:

Desired result:

在此处输入图像描述

You can use the python standard library Tkinter to show an image in a transparent window.

Code Snippet:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM