GTK Python PNG 图像的透明背景
GTK Python Transparent Background for PNG Image
我正在使用 Python 和 GTK 3 将图像叠加到 GStreamer 视频流上,并且我正在尝试使用 Gtk.Overlay() 小部件来执行此操作。该图像是具有透明背景的 .png 文件。我可以很好地叠加图像,但是当我叠加图像时,它有一个白色方块作为背景,而不是在下面显示视频流。
代码片段:
overlay = Gtk.Overlay()
# the drawing area holds the Gstreamer video stream
drawing_area = Gtk.DrawingArea()
drawing_area.set_double_buffered(True)
overlay.add(drawing_area)
# set the drawing area as the base layer of the overlay
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename='icon.png', width=240, height=240, preserve_aspect_ratio=True)
img = Gtk.Image.new_from_pixbuf(pixbuf)
img.set_halign(Gtk.Align.START)
img.set_valign(Gtk.Align.END)
overlay.add_overlay(img)
# overlay the image on the base layer (the video)
完整代码:
from gi.repository import Gdk, Gst, Gtk, GdkX11, GstVideo, GdkPixbuf
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
def window_closed(widget, event, pipeline):
widget.hide()
pipeline.set_state(Gst.State.NULL)
Gtk.main_quit()
if __name__ == "__main__":
Gdk.init([])
Gtk.init([])
Gst.init([])
pipeline = Gst.Pipeline()
# configuring the pipeline code here,
# uses nvarguscamerasrc on the Jetson Nano
# Creating the window
window = Gtk.Window()
window.connect("delete-event", window_closed, pipeline)
window.set_default_size(1920, 1080)
window.set_title("Application")
overlay = Gtk.Overlay()
# the drawing area holds the Gstreamer video stream
drawing_area = Gtk.DrawingArea()
drawing_area.set_double_buffered(True)
overlay.add(drawing_area)
# set the drawing area as the base layer of the overlay
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename='icon.png', width=240, height=240, preserve_aspect_ratio=True)
img = Gtk.Image.new_from_pixbuf(pixbuf)
img.set_halign(Gtk.Align.START)
img.set_valign(Gtk.Align.END)
overlay.add_overlay(img)
# overlay the image on the base layer (the video)
window.add(overlay)
window.show_all()
window.realize()
xid = drawing_area.get_window().get_xid()
sink.set_window_handle(xid)
# set the GStreamer video sink to the drawing area in the window
Gtk.main()
输出:
谢谢!
您可以尝试使用 this composite(...)
方法设置 alpha :
pixbuf.composite(pixbuf, 0, 0, 240, 240, 0, 0, 1, 1, GdkPixbuf.InterpType.INTERP_NEAREST, 0)
我正在使用 Python 和 GTK 3 将图像叠加到 GStreamer 视频流上,并且我正在尝试使用 Gtk.Overlay() 小部件来执行此操作。该图像是具有透明背景的 .png 文件。我可以很好地叠加图像,但是当我叠加图像时,它有一个白色方块作为背景,而不是在下面显示视频流。
代码片段:
overlay = Gtk.Overlay()
# the drawing area holds the Gstreamer video stream
drawing_area = Gtk.DrawingArea()
drawing_area.set_double_buffered(True)
overlay.add(drawing_area)
# set the drawing area as the base layer of the overlay
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename='icon.png', width=240, height=240, preserve_aspect_ratio=True)
img = Gtk.Image.new_from_pixbuf(pixbuf)
img.set_halign(Gtk.Align.START)
img.set_valign(Gtk.Align.END)
overlay.add_overlay(img)
# overlay the image on the base layer (the video)
完整代码:
from gi.repository import Gdk, Gst, Gtk, GdkX11, GstVideo, GdkPixbuf
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)
def window_closed(widget, event, pipeline):
widget.hide()
pipeline.set_state(Gst.State.NULL)
Gtk.main_quit()
if __name__ == "__main__":
Gdk.init([])
Gtk.init([])
Gst.init([])
pipeline = Gst.Pipeline()
# configuring the pipeline code here,
# uses nvarguscamerasrc on the Jetson Nano
# Creating the window
window = Gtk.Window()
window.connect("delete-event", window_closed, pipeline)
window.set_default_size(1920, 1080)
window.set_title("Application")
overlay = Gtk.Overlay()
# the drawing area holds the Gstreamer video stream
drawing_area = Gtk.DrawingArea()
drawing_area.set_double_buffered(True)
overlay.add(drawing_area)
# set the drawing area as the base layer of the overlay
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename='icon.png', width=240, height=240, preserve_aspect_ratio=True)
img = Gtk.Image.new_from_pixbuf(pixbuf)
img.set_halign(Gtk.Align.START)
img.set_valign(Gtk.Align.END)
overlay.add_overlay(img)
# overlay the image on the base layer (the video)
window.add(overlay)
window.show_all()
window.realize()
xid = drawing_area.get_window().get_xid()
sink.set_window_handle(xid)
# set the GStreamer video sink to the drawing area in the window
Gtk.main()
输出:
谢谢!
您可以尝试使用 this composite(...)
方法设置 alpha :
pixbuf.composite(pixbuf, 0, 0, 240, 240, 0, 0, 1, 1, GdkPixbuf.InterpType.INTERP_NEAREST, 0)