我正在使用 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()
这是我的输出:
对于此图像:
有人知道为什么有几张相同的图片而不是一张吗?为什么我必须改变位置 (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/