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

Viewed 1010

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:

enter image description here

Current result:

Desired result:

enter image description here

1 Answers

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