我正在开发一款Kivy应用,让用户可以在模板上定位图像和文字标签。完成后,将裁剪生成的图像以适合模板。
我需要清除图像中的所有空白区域,因为我将打印图像并且不想浪费墨水。然而,Kivy用黑色填充所有空白区域。有没有办法让Kivy的export_to_png()函数使用透明背景而不是黑色背景?
答案 0 :(得分:2)
我可能错了,但看起来透明色是硬编码的。
查看source code似乎ClearColor
被硬编码为黑色。
def export_to_png(self, filename, *args):
'''Saves an image of the widget and its children in png format at the
specified filename. Works by removing the widget canvas from its
parent, rendering to an :class:`~kivy.graphics.fbo.Fbo`, and calling
:meth:`~kivy.graphics.texture.Texture.save`.
.. note::
The image includes only this widget and its children. If you want
to include widgets elsewhere in the tree, you must call
:meth:`~Widget.export_to_png` from their common parent, or use
:meth:`~kivy.core.window.WindowBase.screenshot` to capture the whole
window.
.. note::
The image will be saved in png format, you should include the
extension in your filename.
.. versionadded:: 1.9.0
'''
if self.parent is not None:
canvas_parent_index = self.parent.canvas.indexof(self.canvas)
self.parent.canvas.remove(self.canvas)
fbo = Fbo(size=self.size, with_stencilbuffer=True)
with fbo:
ClearColor(0, 0, 0, 1)
ClearBuffers()
Scale(1, -1, 1)
Translate(-self.x, -self.y - self.height, 0)
我想您可以尝试更新Widget模块代码以接受参数来设置ClearColor
。