Search code examples
pythonarraysmatlabkivytextures

kivy texture from array print many times


I have a picture in an array from matlab. I whant to print this picture in kivy.

# encoding: utf-8

import time
import matlab.engine

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle
from kivy.graphics.texture import Texture

engine = matlab.engine.start_matlab("-nodesktop -noFigureWindows")


class kivyTest(Widget):

    def __init__(self):
        super(kivyTest, self).__init__()
        self.texture = Texture.create(size=(560, 420), colorfmt="rgb")
        time_eng, data_pict = engine.test_pict_data(4.0, 5.0, nargout=2)
        self.texture.blit_buffer(data_pict._data, bufferfmt="ubyte", colorfmt="rgb")
        with self.canvas:
            Rectangle(pos=self.pos, size=(560, 420), texture=self.texture)

    def on_touch_down(self, touch):
        x = touch.pos[0]
        y = touch.pos[1]
        time_eng, data_pict = engine.test_pict_data(x, y, nargout=2)
        self.texture.blit_buffer(data_pict._data, bufferfmt="ubyte", colorfmt="rgb")
        super().on_touch_down(touch)
        self.canvas.ask_update()


class my_app(App):
    title = 'Matlab raw data picture test'
    def build(self):
        return kivyTest()


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

the data_pict._data size is 560x420x3 uint8 in a 1D array But when I print here is the result :

Strange picture print multi tiles

The normal result should be something like : Matlab sin

Do you have any idea about this behaviour ?


Solution

  • Ok I found the way

    I fact the 1D data from matlab (pictureFrame.cdata._data) is not usable as this, we need to reorganize it with numpy and flat it when pass to the texture.

    data_reorg = np.array(data_pict._data).reshape(data_pict.size, order='F')
    self.texture.blit_buffer(data_reorg.ravel(), bufferfmt="ubyte", colorfmt="rgb")
    

    This way is fast and efficient.