PyGTK 中的像素图透明度

发布于 2024-07-24 03:29:17 字数 410 浏览 1 评论 0原文

如何创建将一个像素值设置为透明的 PyGTK 像素图? 我知道它与创建深度为 1 的像素图并将其设置为遮罩有关,但我发现它要么不执行任何操作,要么在绘制时完全擦除我的像素图。 目前,我制作了一个像素图,

r = self.get_allocation()
p1 = gtk.gdk.Pixmap(self.window,r.width,r.height)
p1_c = p1.cairo_create()

然后使用 Cairo 在其上绘制黑色线条。 我想要做的是让所有未被线条覆盖的区域透明(例如,使白色成为透明颜色),这样当我用draw_drawable将其绘制到窗口时,它会将所有内容留在“下面”完好无损的。

有关此问题的常见问题解答和邮件列表帖子毫无帮助,因为它们已经过时了。 这里一定有人知道!

How can I create PyGTK pixmaps with one pixel value set to transparent? I know it has something to do with creating a pixmap of depth 1 and setting it as a mask, but all I find is that it either does nothing or totally erases my pixmap when drawn. At the moment, I make a pixmap with

r = self.get_allocation()
p1 = gtk.gdk.Pixmap(self.window,r.width,r.height)
p1_c = p1.cairo_create()

then draw black lines all over it using Cairo. What I'd like to be able to do is to have all of the area not covered by lines transparent (making white the transparent colour, say), so that when I draw it to the window with draw_drawable, it leaves everything 'underneath' intact.

The FAQs and mailing list postings regarding this issue are most unhelpful as they are so outdated. Someone must know here!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月野兔 2024-07-31 03:29:17

我认为您无法使用 PixmapPixbuf 完成您想要的操作,但这里有两种在现有 Widget 之上实现涂鸦的策略代码>. 最明显的一个是捕获绘制事件并直接绘制到 WidgetDrawable 上,中间不保留图像:

from gtk import Window, Button, main
from math import pi
import cairo

w = Window()
b = Button("Draw on\ntop of me!")

def scribble_on(cr):
    cr.set_source_rgb(0, 0, 0)
    cr.rectangle(10, 10, 30, 30)
    cr.fill()
    cr.arc(50, 50, 10, 0, pi)
    cr.stroke()

def expose_handler(widget, event):
    cr = widget.window.cairo_create()
    cr.rectangle(event.area.x, event.area.y,
                 event.area.width, event.area.height)
    cr.clip()
    scribble_on(cr)
    return False

b.connect_after("expose_event", expose_handler)
w.add(b)
w.set_size_request(100, 100)
w.show_all()
main()

如果您愿意,可以选择第二个选项要获得无需在每次请求重绘时更新的中间 ARGB 图像,可以将图像预渲染到 ImageSurface。 这是上面的 expose_handler 的替代品,它只绘制一次图像:

import cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
scribble_on(cairo.Context(surface))

def expose_image_handler(widget, event):
    cr = widget.window.cairo_create()
    cr.rectangle(event.area.x, event.area.y,
                 event.area.width, event.area.height)
    cr.clip()
    cr.set_source_surface(surface)
    cr.paint()

如果这是您正在寻找的东西,我建议更新问题的标题以反映您的真正需求: )。

I don't think you can do what you want with a Pixmap or Pixbuf, but here are two strategies for implementing scribbling on top of an existing Widget. The most obvious one is just to catch the draw event and draw straight onto the Widget's Drawable, with no retained image in the middle:

from gtk import Window, Button, main
from math import pi
import cairo

w = Window()
b = Button("Draw on\ntop of me!")

def scribble_on(cr):
    cr.set_source_rgb(0, 0, 0)
    cr.rectangle(10, 10, 30, 30)
    cr.fill()
    cr.arc(50, 50, 10, 0, pi)
    cr.stroke()

def expose_handler(widget, event):
    cr = widget.window.cairo_create()
    cr.rectangle(event.area.x, event.area.y,
                 event.area.width, event.area.height)
    cr.clip()
    scribble_on(cr)
    return False

b.connect_after("expose_event", expose_handler)
w.add(b)
w.set_size_request(100, 100)
w.show_all()
main()

A second option, if you want to have an intermediary ARGB image that you don't have to update each time a redraw is requested, would be to pre-render the image to an ImageSurface. Here's a replacement for expose_handler, above, that only draws the image once:

import cairo
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
scribble_on(cairo.Context(surface))

def expose_image_handler(widget, event):
    cr = widget.window.cairo_create()
    cr.rectangle(event.area.x, event.area.y,
                 event.area.width, event.area.height)
    cr.clip()
    cr.set_source_surface(surface)
    cr.paint()

If this is the sort of thing you're looking for, I would recommend updating the title of the question to reflect your real need :).

≈。彩虹 2024-07-31 03:29:17

看起来您想使用 Pixbuf 而不是 Pixmap。 Pixbuf 包含一个 alpha 设置,它将为您提供透明度,而 Pixmap 则不然。

It looks like you want to use a Pixbuf and not a Pixmap. The Pixbuf includes an alpha setting, which will give you transparency, whereas the Pixmap does not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文