4

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

Vamsi
  • 103
  • 1
  • 1
  • 7
  • cv2.imshow ignores transparency. You can use matplotlib's imshow. https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imshow.html – Farhood ET Apr 10 '21 at 12:38
  • @FarhoodET Matplotlib's imshow could be used if we want to make the image transparent by having a four channel image or by using the `alpha` argument. But I don't think that we could make the window or the canvas transparent. – Kishore Sampath Apr 10 '21 at 12:56
  • Try Python Wand, which uses Imagemagick. Imagemagick can display transparent images. – fmw42 Apr 20 '23 at 15:51

2 Answers2

2

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()
Kishore Sampath
  • 973
  • 6
  • 13
1

Seems it works with qt on linux source
It's looks like u don't really need Qt.WA_NoSystemBackground
Use Qt.FramelessWindowHint to make frames disappear.

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel

app = QApplication(sys.argv)

window = QMainWindow()

window.setAttribute(Qt.WA_TranslucentBackground, True)
# window.setAttribute(Qt.WA_NoSystemBackground, True)
# window.setWindowFlags(Qt.FramelessWindowHint)

label = QLabel(window)
pixmap = QPixmap('po.png')
label.setPixmap(pixmap)
label.setGeometry(10, 10, pixmap.width(), pixmap.height())

window.label = label

window.resize(pixmap.width(),pixmap.height())

window.show()
sys.exit(app.exec_())

po

Flinck Clissan
  • 104
  • 1
  • 5