【问题标题】:Kivy-python: duplicate image on textureKivy-python:纹理上的重复图像
【发布时间】:2020-01-29 09:18:39
【问题描述】:

我正在使用 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()

这是我的输出:

对于这张图片:

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

最好的问候!

【问题讨论】:

    标签: python python-3.x numpy opencv kivy


    【解决方案1】:

    我认为问题在于您在阅读图像时将图像转换为灰度,然后您将rgb 用于Texture 颜色格式。如果您让这两个同意,那么您的代码将起作用。例如,改变:

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

    到:

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

    【讨论】:

      猜你喜欢
      • 2020-03-04
      • 1970-01-01
      • 2010-09-30
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多