如何标记透明的pygtkpycairo窗口并保存合并后的图像?

2024-06-26 03:55:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试开发一个透明窗口,在这个窗口上我可以标记屏幕上的任何内容(包括动态的)。 我的最终目标是在在线科学出版物上覆盖图表,点击并沿着曲线累积点 并最终使用生成器函数对采集的点进行曲线拟合。 这将包括建立线和轴,记号,和其他好处。 不过,我尽量保持问题代码非常简单。在

下面的代码对透明部分做得相当好(请批评并更正)。 我做了大量的研究来理解如何保存透明窗口的内容,但是失败了。 我试图找出如何覆盖任何东西(绘图原语),但失败了。 我寻求任何和所有的建议来推进这个项目,并打算使最终的代码开源。在

请帮忙。在


    #!/usr/bin/env python
    """
    trans.py Transparent window with markup capability.
    Goals:
        1. make a transparent window that dynamically updates (working).
        2. draw opaque points, lines, text, and more (unimplemented).
        3. save window overlayed by opaque points to png (unimplemented).
        4. toggle overlay on/off (unimplemented).
        5. make cursor XOR of CROSSHAIR (unimplemented).
    """

    import pygtk
    pygtk.require('2.0')
    import gtk, cairo

    class Transparency(object):
        def __init__(self, widget, index):
            self.xy = widget.get_size()
            self.cr = widget.window.cairo_create()
            self.index = index
        def __enter__(self):
            self.cr.set_operator(cairo.OPERATOR_CLEAR)
            self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, *self.xy)
            self.cr.rectangle(0.0, 0.0, *self.xy)
            self.cr.fill()
            return self.cr, self.surface
        def __exit__( self, exc_type, exc_val, exc_tb ):
            filename = '%08d.png' % (self.index)
            with open(filename, 'w+') as png:
                self.surface.write_to_png(png)
                print filename
            self.cr.set_operator(cairo.OPERATOR_OVER)

    class Expose(object):
        def __init__(self):
            self.index = 0
        def __call__(self, widget, event):
            with Transparency(widget, self.index) as (cr, surface):
                # cr and surface are available for drawing.
                pass
            self.index += 1

    def main():
        x, y = 201, 201
        win = gtk.Window(gtk.WINDOW_TOPLEVEL)
        win.connect("destroy", lambda w: gtk.main_quit())
        win.set_decorated(True)
        win.set_app_paintable(True)
        win.set_size_request(x, y)
        win.set_colormap(win.get_screen().get_rgba_colormap())
        win.connect('expose-event', Expose())
        win.realize()
        win.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.DIAMOND_CROSS))
        win.show()
        gtk.main()

    if __name__ == "__main__":
        main()

更新!我需要做的大部分工作除了大的。 如何保存由底层窗口和透明覆盖组成的图像? 使用vi风格的纯键盘控件,可以对点进行分层和切换覆盖。 以下是最新消息来源:

^{pr2}$

Tags: 代码selfgtkindexpngmaindefwidget
1条回答
网友
1楼 · 发布于 2024-06-26 03:55:08

这不能回答你的具体问题,但我想:为什么要重新发明轮子?我的建议是使用高级截图应用程序,比如Shutter来捕捉窗口或选择。Shutter将为您提供一个可浏览的“已保存”窗口的图库,具有编辑和web发布功能,并根据用户定义的配置文件自动将图像存储在专用文件夹中(按项目、所需分辨率等)。在

相关问题 更多 >