<i id='lQb12'><tr id='lQb12'><dt id='lQb12'><q id='lQb12'><span id='lQb12'><b id='lQb12'><form id='lQb12'><ins id='lQb12'></ins><ul id='lQb12'></ul><sub id='lQb12'></sub></form><legend id='lQb12'></legend><bdo id='lQb12'><pre id='lQb12'><center id='lQb12'></center></pre></bdo></b><th id='lQb12'></th></span></q></dt></tr></i><div id='lQb12'><tfoot id='lQb12'></tfoot><dl id='lQb12'><fieldset id='lQb12'></fieldset></dl></div>
<legend id='lQb12'><style id='lQb12'><dir id='lQb12'><q id='lQb12'></q></dir></style></legend>
  • <tfoot id='lQb12'></tfoot>
      <bdo id='lQb12'></bdo><ul id='lQb12'></ul>

      1. <small id='lQb12'></small><noframes id='lQb12'>

        在 Kivy 中显示一个 numpy 数组

        时间:2023-06-07
          <tbody id='4aHwq'></tbody>
        <tfoot id='4aHwq'></tfoot>

        1. <i id='4aHwq'><tr id='4aHwq'><dt id='4aHwq'><q id='4aHwq'><span id='4aHwq'><b id='4aHwq'><form id='4aHwq'><ins id='4aHwq'></ins><ul id='4aHwq'></ul><sub id='4aHwq'></sub></form><legend id='4aHwq'></legend><bdo id='4aHwq'><pre id='4aHwq'><center id='4aHwq'></center></pre></bdo></b><th id='4aHwq'></th></span></q></dt></tr></i><div id='4aHwq'><tfoot id='4aHwq'></tfoot><dl id='4aHwq'><fieldset id='4aHwq'></fieldset></dl></div>

            <bdo id='4aHwq'></bdo><ul id='4aHwq'></ul>
              • <small id='4aHwq'></small><noframes id='4aHwq'>

                <legend id='4aHwq'><style id='4aHwq'><dir id='4aHwq'><q id='4aHwq'></q></dir></style></legend>
                  本文介绍了在 Kivy 中显示一个 numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  首先,我对kivy完全陌生,所以我有点挣扎.

                  first of all, I'm totally new to kivy, so I'm struggling a bit.

                  我正在尝试在 kivy 窗口中显示一个 numpy 数组.到目前为止,我发现这应该使用纹理类(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 数组不时发生变化,我正在尝试将以下代码调整为我的应用程序.

                  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))
                  

                  似乎创建纹理并对其进行更改可以正常工作,但我不明白如何显示纹理.

                  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 数组.

                  in a way, that I get to see my picture/numpy array.

                  提前非常感谢!Holzroller

                  Thanks alot in advance! Holzroller

                  我发现使用 Kivy 1.8.0 和纹理类有点乱.所以我通过 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)
                  

                  基本上我回到了原来的问题:我有一个 numpy 数组(类型 'numpy.ndarray'; dtype 'uint8'),我正在尝试将其转换为一种格式,以便纹理向我显示图像.我试图将其分解为与我在上面发布的示例代码中相同的方式.但我很遗憾没有工作.我真的不知道我在这里做错了什么.(我的 numpy 数组在以下代码中称为 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 有一个 tostring() 属性,如果源数组是 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"
                  

                  关于你在评论中谈论的问题,我看到 2 点:

                  About the issue you're talking in the comment, i see 2 points:

                  1. 确保在主线程中调用来自 ROS 的回调.也许更新被忽略了.
                  2. 当您手动更改纹理时,不会通知使用它的关联对象,您需要这样做.添加一个 self.canvas.ask_update() 以确保画布在下一帧重新显示.

                  这篇关于在 Kivy 中显示一个 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python Kivy ListView:如何删除选定的 ListItemButton? 下一篇:PyInstalled Kivy 应用程序无法在第二台机器上运行

                  相关文章

                  1. <legend id='Ny0f3'><style id='Ny0f3'><dir id='Ny0f3'><q id='Ny0f3'></q></dir></style></legend>

                    <small id='Ny0f3'></small><noframes id='Ny0f3'>

                    <i id='Ny0f3'><tr id='Ny0f3'><dt id='Ny0f3'><q id='Ny0f3'><span id='Ny0f3'><b id='Ny0f3'><form id='Ny0f3'><ins id='Ny0f3'></ins><ul id='Ny0f3'></ul><sub id='Ny0f3'></sub></form><legend id='Ny0f3'></legend><bdo id='Ny0f3'><pre id='Ny0f3'><center id='Ny0f3'></center></pre></bdo></b><th id='Ny0f3'></th></span></q></dt></tr></i><div id='Ny0f3'><tfoot id='Ny0f3'></tfoot><dl id='Ny0f3'><fieldset id='Ny0f3'></fieldset></dl></div>
                    <tfoot id='Ny0f3'></tfoot>

                      <bdo id='Ny0f3'></bdo><ul id='Ny0f3'></ul>