7

I'm trying to draw an image with a transparent background over the entire screen. I've done similar with Xlib, but I thought I'd try Qt so my program might work in different environments besides X11. Before getting committed, I've honestly just searched the internet trying to find a working example but failed.

So I've got an image of coordinates that I want to overlay on the screen. The image is a PNG with a transparent background. Here's a picture of what I want to see, my desktop with coordinates: enter image description here

Here's my PyQt6 code, it draws the image fullscreen but sadly you can't see through it, it has a black background:

import sys
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QImage
from PyQt6.QtWidgets import QApplication, QMainWindow

from PyQt6.QtGui import QPainter

class Img(QMainWindow):
    def __init__(self, img_path, parent=None):
        super().__init__(parent)
        self.qimg = QImage(img_path)
        self.setStyleSheet('QMainWindow {background:transparent}')
        self.setWindowFlags(
            Qt.WindowType.WindowStaysOnTopHint |
            Qt.WindowType.FramelessWindowHint |
            Qt.WindowType.WindowTransparentForInput
        )
        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)

    def paintEvent(self, qpaint_event):
        painter = QPainter(self)
        rect = qpaint_event.rect()
        painter.drawImage(rect, self.qimg)
        self.showFullScreen()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    img_path = 'coords.png'
    window = Img(img_path)
    window.show()
    sys.exit(app.exec())

Answers don't need to be PyQt6, perhaps PyQt5 or written in Go.

GGG
  • 295
  • 1
  • 9
  • 1
    Could you explain the problem further? I can't understand what you want to achieve, when I run the program I get a window with an image that covers everything and that's apparently what you are looking for, isn't it? – WhoKnowsMe Aug 16 '22 at 19:51
  • @WhoKnowsMe I've rewritten my question, importantly my image has transparent parts and you should be able to see your desktop through them. Can you? Thanks. – GGG Aug 18 '22 at 10:06

1 Answers1

3

I have tried your code and it works fine, apparently it seems to be a problem of the image, I recommend you to use some kind of external application to erase the transparent parts like this.

Here is a test that works with your code:enter image description here

If you want to do this automatically I recommend you to use opencv.

WhoKnowsMe
  • 498
  • 2
  • 13
  • 1
    Thank you, knowing this I thought it must be my window manager (`bspwm`), and I installed and ran a compositor (`picom`) and it works :) – GGG Aug 19 '22 at 08:23
  • 1
    And I'll have a look at opencv. – GGG Aug 19 '22 at 08:26
  • 1
    I recommend you to checkout [this SO question](https://stackoverflow.com/questions/40527769/removing-black-background-and-make-transparent-from-grabcut-output-in-python-ope) and [this other from GeeksforGeeks](https://www.geeksforgeeks.org/removing-black-background-and-make-transparent-using-python-opencv/) – WhoKnowsMe Aug 19 '22 at 10:06