ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 将Gtk.DrawingArea或Cairo模式的内容保存到磁盘上的映像

2019-10-03 06:07:48  阅读:346  来源: 互联网

标签:cairo python gtk pygobject pycairo


我有一个使用Cairo image surface的小型PyGI项目,然后我用surface pattern进行缩放并在Gtk.DrawingArea上渲染.

我想将缩放版本写入PNG文件.我试图用Surface.write_to_png()从原始表面写入,但它只写入原始(即非缩放)大小,所以我卡在那里.

然后我想我可以从Gtk.DrawingArea获取渲染的图像并将其写入磁盘,但我还没有发现如何在PyGI中做到这一点(这似乎只能在GTK 2 – save gtk.DrawingArea to file中实现).所以我想弄清楚如何将我的缩放图像写入磁盘.

这是创建曲面,缩放并渲染曲面的代码:

def on_drawingarea1_draw (self, widget, ctx, data=None):

    # 'widget' is a Gtk.DrawingArea
    # 'ctx' is the Cairo context

    text = self.ui.entry1.get_text()
    if text == '':
        return

    # Get the data and encode it into the image
    version, size, im = qrencode.encode(text)
    im = im.convert('RGBA') # Cairo expects RGB

    # Create a pixel array from the PIL image
    bytearr = array.array('B', im.tostring())
    height, width = im.size

    # Convert the PIL image to a Cairo surface
    self.surface = cairo.ImageSurface.create_for_data(bytearr,
                                                 cairo.FORMAT_ARGB32,
                                                 width, height,
                                                 width * 4)

    # Scale the image
    imgpat = cairo.SurfacePattern(self.surface)

    scaler = cairo.Matrix()
    scaler.scale(1.0/self.scale_factor, 1.0/self.scale_factor)
    imgpat.set_matrix(scaler)
    ctx.set_source(imgpat)

    # Render the image
    ctx.paint()

这是将表面写入PNG文件的代码:

def on_toolbuttonSave_clicked(self, widget, data=None):
    if not self.surface:
        return

    # The following two lines did not seem to work
    # ctx = cairo.Context(self.surface)
    # ctx.scale(self.scale_factor, self.scale_factor)

    self.surface.write_to_png('/tmp/test.png')

因此,写表面会创建一个非缩放图像,并且cairo.SurfacePattern中也没有写入方法.

我最后的办法是获取gtk.DrawingArea中呈现的缩放图像,将其放入GtkPixbuf.Pixbuf或新表面,然后将其写入磁盘. pixbuf方法似乎适用于GTK 2,但不适用于GTK 3.

那么有谁知道如何将缩放的图像写入磁盘?

解决方法:

好的,我找到了一种方法:

记住Gtk.DrawingArea派生自Gtk.Window,我可以使用Gdk.pixbuf_get_from_window()函数将绘图区域的内容放到GdkPixbuf.Pixbuf中,然后使用GdkPixbuf.Pixbuf.savev()函数编写pixbuf作为磁盘上的图像.

def drawing_area_write(self):
    # drawingarea1 is a Gtk.DrawingArea
    window = self.ui.drawingarea1.get_window()

    # Some code to get the coordinates for the image, which is centered in the
    # in the drawing area. You can ignore it for the purpose of this example
    src_x, src_y = self.get_centered_coordinates(self.ui.drawingarea1,
                                                 self.surface)
    image_height = self.surface.get_height() * self.scale_factor
    image_width = self.surface.get_width() * self.scale_factor

    # Fetch what we rendered on the drawing area into a pixbuf
    pixbuf = Gdk.pixbuf_get_from_window(window, src_x, src_y,
                                      image_width, image_height)

    # Write the pixbuf as a PNG image to disk
    pixbuf.savev('/tmp/testimage.png', 'png', [], [])

虽然这有效,但看到有人能够证实这是正确的方法或者看看是否还有其他替代方案仍然很好.

标签:cairo,python,gtk,pygobject,pycairo
来源: https://codeday.me/bug/20191003/1847307.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有