How to save a kivy canvas object as an image file
Question:
I was wondering if it is possible to save a canvas that had several textures painted on it as an image file.
I know I can save regular Image’s (kivy.core.image) or Texture’s (kivy.graphics.texture) as an image file with the save() function, so if I am able to convert the canvas to an Image or a Texture it should be easy, but so far I wasn’t able to do this.
Answers:
Widgets have an export_to_png
method. Call this from the Widget whose canvas you have drawn on.
The problem is that your MyPainter
widget has its default size of (100,100)
and its default position (0,0)
. So it is actually behind your Save
Button
. So all your drawing is actually outside the MyPainter
widget, and saving the widget to an image is blank.
The fix is to change the pos
and size
of MyPainter
, and perhaps use collide_point()
in your on_touch_down()
and on_touch_move()
methods to ensure that you are actually drawing on the MyPainter
widget.
I was wondering if it is possible to save a canvas that had several textures painted on it as an image file.
I know I can save regular Image’s (kivy.core.image) or Texture’s (kivy.graphics.texture) as an image file with the save() function, so if I am able to convert the canvas to an Image or a Texture it should be easy, but so far I wasn’t able to do this.
Widgets have an export_to_png
method. Call this from the Widget whose canvas you have drawn on.
The problem is that your MyPainter
widget has its default size of (100,100)
and its default position (0,0)
. So it is actually behind your Save
Button
. So all your drawing is actually outside the MyPainter
widget, and saving the widget to an image is blank.
The fix is to change the pos
and size
of MyPainter
, and perhaps use collide_point()
in your on_touch_down()
and on_touch_move()
methods to ensure that you are actually drawing on the MyPainter
widget.