python – 将Gtk.DrawingArea或Cairo模式的内容保存到磁盘上的映像
作者:互联网
我有一个使用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