問題描述
首先,我對kivy完全陌生,所以我有點(diǎn)掙扎.
first of all, I'm totally new to kivy, so I'm struggling a bit.
我正在嘗試在 kivy 窗口中顯示一個(gè) numpy 數(shù)組.到目前為止,我發(fā)現(xiàn)這應(yīng)該使用紋理類(http://kivy.org/docs/api-kivy.graphics.texture.html).
I'm trying to display a numpy array in a kivy window. So far i figured out that this should work using the Texture Class (http://kivy.org/docs/api-kivy.graphics.texture.html).
隨著我的 numpy 數(shù)組不時(shí)發(fā)生變化,我正在嘗試將以下代碼調(diào)整為我的應(yīng)用程序.
As my numpy array changes from time to time, I'm trying to adjust the following code to my application.
# create a 64x64 texture, defaults to rgb / ubyte
texture = Texture.create(size=(64, 64))
# create 64x64 rgb tab, and fill with values from 0 to 255
# we'll have a gradient from black to white
size = 64 * 64 * 3
buf = [int(x * 255 / size) for x in range(size)]
# then, convert the array to a ubyte string
buf = b''.join(map(chr, buf))
# then blit the buffer
texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte')
# that's all ! you can use it in your graphics now :)
# if self is a widget, you can do this
with self.canvas:
Rectangle(texture=texture, pos=self.pos, size=(64, 64))
似乎創(chuàng)建紋理并對其進(jìn)行更改可以正常工作,但我不明白如何顯示紋理.
It seems that creating the texture and changing it works as it should, but i dont get, how to display the texture.
誰能給我解釋一下,如何使用
Can anybody explain to me, how to use the
with self.canvas:
Rectangle(texture=texture, pos=self.pos, size=(64, 64))
在某種程度上,我可以看到我的圖片/numpy 數(shù)組.
in a way, that I get to see my picture/numpy array.
提前非常感謝!Holzroller
Thanks alot in advance! Holzroller
我發(fā)現(xiàn)使用 Kivy 1.8.0 和紋理類有點(diǎn)亂.所以我通過 github 升級到 Kivy 1.9.0(在 Ubuntu 14.04 LTS 中通過 apt-get 安裝 Kivy 為您提供 1.8.0 版本),我可以使用以下代碼查看紋理.希望對和我有同樣問題的人有所幫助.
I figured out that using Kivy 1.8.0 and the Texture Class is a bit messy. So I upgraded to Kivy 1.9.0 via github (installing Kivy via apt-get in Ubuntu 14.04 LTS serves you the 1.8.0 version) and I get to see the Texture using the following code. I hope that helps people who are having the same problem as me.
from kivy.graphics.texture import Texture
from kivy.graphics import Rectangle
from kivy.uix.widget import Widget
from kivy.base import runTouchApp
from array import array
from kivy.core.window import Window
# create a 64x64 texture, defaults to rgb / ubyte
texture = Texture.create(size=(1280, 1024), colorfmt='rgb')
# create 64x64 rgb tab, and fill with values from 0 to 255
# we'll have a gradient from black to white
size = 1280 * 1024 * 3
buf = [int(x * 255 / size) for x in range(size)]
# then, convert the array to a ubyte string
arr = array('B', buf)
# buf = b''.join(map(chr, buf))
# then blit the buffer
texture.blit_buffer(arr, colorfmt='rgb', bufferfmt='ubyte')
# that's all ! you can use it in your graphics now :)
# if self is a widget, you can do this
root = Widget()
with root.canvas:
Rectangle(texture=texture, pos=(0, 0), size=(1280*3, 1024*3))
runTouchApp(root)
基本上我回到了原來的問題:我有一個(gè) numpy 數(shù)組(類型 'numpy.ndarray'; dtype 'uint8'),我正在嘗試將其轉(zhuǎn)換為一種格式,以便紋理向我顯示圖像.我試圖將其分解為與我在上面發(fā)布的示例代碼中相同的方式.但我很遺憾沒有工作.我真的不知道我在這里做錯(cuò)了什么.(我的 numpy 數(shù)組在以下代碼中稱為 im2)
Basically I'm back to the original Problem: I have a numpy array (type 'numpy.ndarray'; dtype 'uint8') and I'm trying to convert it into a format, so that the texture will show me the image. I tried to break it down to the same way it is done in the example code i posted above. But i sadly doesn't work. I really do not know what I'm doing wrong here. (my numpy array is called im2 in the folling code)
list1 = numpy.array(im2).reshape(-1,).tolist()
arr = array('B', list1)
texture.blit_buffer(arr, colorfmt='rgb', bufferfmt='ubyte')
推薦答案
Numpy 有一個(gè) tostring()
屬性,如果源數(shù)組是 uint8 類型,您可以直接使用該屬性.你甚至不需要重塑:
Numpy have a tostring()
attribute, that you could use directly, if the source array is uint8 type. You don't even need to reshape:
texture = Texture.create(size=(16, 16), colorfmt="rgb"))
arr = numpy.ndarray(shape=[16, 16, 3], dtype=numpy.uint8)
# fill your numpy array here
data = arr.tostring()
texture.blit_buffer(data, bufferfmt="ubyte", colorfmt="rgb"
關(guān)于你在評論中談?wù)摰膯栴},我看到 2 點(diǎn):
About the issue you're talking in the comment, i see 2 points:
- 確保在主線程中調(diào)用來自 ROS 的回調(diào).也許更新被忽略了.
- 當(dāng)您手動更改紋理時(shí),不會通知使用它的關(guān)聯(lián)對象,您需要這樣做.添加一個(gè) self.canvas.ask_update() 以確保畫布在下一幀重新顯示.
這篇關(guān)于在 Kivy 中顯示一個(gè) numpy 數(shù)組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!