在 Python 中显示透明且无背景的图像或 window

Display an image with transparency and no background or window in Python

我正在尝试在屏幕上显示图像,但没有任何 window/application 弹出 up/containing 它。我与 TKinter 非常接近,但是删除 canvas 背景颜色的方法很老套,并且会产生一些不良影响。

import tkinter as tk
import ctypes
user32 = ctypes.windll.user32
screen_size = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)

root = tk.Tk()

root.overrideredirect(True)
root.config(bg="blue", bd=0, highlightthickness=0)
root.attributes("-transparentcolor", "#FEFCFD")
root.attributes("-topmost", True)

tk_img = tk.PhotoImage(file="image.png")

canvas = tk.Canvas(root, bg="#FEFCFD", bd=0, highlightthickness=0, width=screen_size[0], height=screen_size[1])
canvas.pack()

img = canvas.create_image(0, 0, image=tk_img, anchor="nw")

root.mainloop()

-transparentcolor 标志主要删除背景,但如果图像有任何部分透明的像素,它将对其进行着色。另外,如果该颜色存在于图像中,它将被删除;这种颜色的选择是希望尽量减少图像中的精确匹配,同时也主要是白色,希望对图像的影响最小。 Here's an image of what it looks like currently; very close to what I want, but you can see some missing pixels in the white areas of the dice, and they all seem to have a white border around them due to their edges being partially transparent. This is what the image should look like.

我也尝试过使用 wxPython 实现这种效果,但我无法删除 window 的背景,导致透明图像总是被某些颜色所支持。我使用 this answer; 我稍微修改了它,但我所做的一切都没有改进它。

那么,有没有办法用 Python 在屏幕上绘制一个完全没有背景的图像?

So, is there a way to draw an image on the screen without any background at all with Python?

使用Tkinter,对于这张图片,不行,你达不到想要的效果。(你可以寻找其他模块,如'PyQT5','Kivy' 、'wxPython' 或 'turtle' 可能。)


参见,transparentcolor指定顶层的透明颜色索引。

如果您想在 Tkinter 中做到最好,请对您的代码进行一些更改:

root.attributes('-transparentcolor', '#d4d4e2')
root.attributes("-topmost", True)
tk_img = tk.PhotoImage(file="image.png")       
canvas = tk.Canvas(root, bg="#d4d4e2", bd=0, highlightthickness=0, width=screen_size[0], height=screen_size[1])

因此,这将显示一个 window,其中包含一个具有透明背景的 canvas。非常接近您想要的结果,您可以看到骰子白色区域中缺失的像素非常少,但这仍然不是解决方案。


but if an image has any partially transparent pixels it will tint them

是的,是的,如果图像中存在该颜色,它将被删除,因为该颜色用于标记需要用作透明色的颜色。


that choice of color was in hopes of minimizing exact matches in an image while also being mostly white, to hopefully have the least noticeable effect on the images.

为了让这张图的edges/borders保留部分'white'透明的背景,颜色的选择需要是'white'的一些阴影。因此,用于使其透明的颜色是 #d4d4e2(对于此图像,只有一个地方使用了此颜色像素,因此它不明显。)不过,边缘会有尖角和切口。

感谢 Kartikeya 的建议,我得以解决自己的问题。

使用 PyQt5,此代码将显示透明且完全没有边框或背景的图像

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('image.png')
label.setPixmap(pixmap)
label.setGeometry(0, 0, pixmap.width(), pixmap.height())

window.label = label

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

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

有一次我在寻找 PyQt5,我找到了 and only needed to modify the code slightly. Here is what it looks like now.