python - Kivy-python : duplicate image on texture

标签 python python-3.x numpy opencv kivy

我正在使用 kivy 在 python 3.6 中开发应用程序。
我想显示保存为 numpy 数组的图像。
我写了这段代码:

from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.graphics.texture import Texture
import cv2

class Test(Widget):
    def __init__(self, **kwargs):
        super(Test, self).__init__(**kwargs)

        img = cv2.imread(r'./kulki.jpg', cv2.IMREAD_GRAYSCALE)
        w, h = img.shape
        texture = Texture.create(size=(h, w))
        texture.blit_buffer(img.flatten(), colorfmt='rgb', bufferfmt='ubyte')
        w_img = Image(size=(w, h), texture=texture)
        self.add_widget(w_img)

class DemoApp(App):
    def build(self):
        return Test()

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

这是我的输出:

enter image description here

对于此图像:

enter image description here

有人知道为什么有几张相同的图片而不是一张吗?为什么我必须改变位置 (w,h) -> (h,w) 的尺寸?

最诚挚的问候!

最佳答案

我认为问题在于您在读取图像时将其转换为灰度,然后您使用 rgb 作为 Texture 颜色格式。如果你让这两者达成一致,那么你的代码就会工作。例如,更改:

texture.blit_buffer(img.flatten(), colorfmt='rgb', bufferfmt='ubyte')

至:

texture.blit_buffer(img.flatten(), colorfmt='luminance', bufferfmt='ubyte')

关于python - Kivy-python : duplicate image on texture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59963881/

相关文章:

python - numpy中 'invalid value encountered in less_equal'的原因可能是什么

python - 在 x64 架构上获得心理加速?

python - 比较两个列表并提取元素

python - 如何在张量 session 中在一行中同时运行多个操作?

python - 使用Python正则表达式可随时从某个字符之后的字符串中提取数字。

python-3.x - 对 DataFrameGroupBy 中的字符串使用 Pandas Rank

python - scipy bisplrep 段错误(核心转储)

python - 找出 90% 的工单得到处理的时间?

python - Google 群组 CAN_REQUEST_TO_JOIN

python - Django 1.4 和 NoReverseMatch 在/admin/error