Kivy, save contents of the Canvas in a StringIO

0

I am trying the following code ( link ):

from random import random
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line


class MyPaintWidget(Widget):

  def on_touch_down(self, touch):
    color = (random(), random(), random())
    with self.canvas:
        Color(*color)
        d = 30.
        Ellipse(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
        touch.ud['line'] = Line(points=(touch.x, touch.y))

  def on_touch_move(self, touch):
    touch.ud['line'].points += [touch.x, touch.y]


class MyPaintApp(App):

 def build(self):
    parent = Widget()
    self.painter = MyPaintWidget()
    clearbtn = Button(text='Clear')
    clearbtn.bind(on_release=self.clear_canvas)
    parent.add_widget(self.painter)
    parent.add_widget(clearbtn)
    return parent

 def clear_canvas(self, obj):
    self.painter.export_to_png("testcanvas.png") #store canvas in a png file 
    self.painter.canvas.clear()


if __name__ == '__main__':
   MyPaintApp().run()

The example works correctly.

I am trying to save the contents I draw in canvas in a file, however when I open the generated file, the image is completely black. I want to save what I draw in the canvas as binary content in a Python StringIO to later send it through a websocket . Initially I am trying to save canvas to an image file.

There is some way to convert the canvas to an image in binary format that can be saved in a StringIO so that later, in the backend, can perform the following conversion:

img_array = np.asarray(bytearray(img_stream.read()), dtype=np.uint8)
    
asked by mxman 01.05.2016 в 19:42
source

0 answers