Kivy截取背景清晰的截图
Kivy take screenshot with clear background
我正在开发一个 Kivy 应用程序,它允许用户在模板上定位图像和文本标签。完成后,生成的图像将被裁剪以适合模板。
我需要图像中的所有空白 space 都清晰,因为我要打印图像并且不想浪费墨水。然而,Kivy 用黑色填充了所有空的 space。有没有办法让 Kivy 的 export_to_png() 函数使用透明背景而不是黑色背景?
我可能是错的,但看起来透明颜色是硬编码的。
查看 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
。
我正在开发一个 Kivy 应用程序,它允许用户在模板上定位图像和文本标签。完成后,生成的图像将被裁剪以适合模板。
我需要图像中的所有空白 space 都清晰,因为我要打印图像并且不想浪费墨水。然而,Kivy 用黑色填充了所有空的 space。有没有办法让 Kivy 的 export_to_png() 函数使用透明背景而不是黑色背景?
我可能是错的,但看起来透明颜色是硬编码的。
查看 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
。