Kivypython:在纹理上复制图像

2024-04-20 09:50:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

致以最良好的祝愿


Tags: fromtest图像imageimportselfappimg
1条回答
网友
1楼 · 发布于 2024-04-20 09:50:09

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

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

致:

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

相关问题 更多 >