如何在kivy中访问原始图像数据?

2024-09-30 18:16:19 发布

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

如何访问kivy.core.image.Image对象的原始图像数据?文档说Image对象的image属性具有图像的原始数据,但当我打印它时,它返回None

这就是我所做的:

#paintwg is the widget

img = paintwg.export_to_image()
print(img.image)

Output:
>>> None

Tags: the数据对象文档core图像imagenone
1条回答
网友
1楼 · 发布于 2024-09-30 18:16:19

^{}

Return an core Image of the actual widget.

^{}

Core classes for loading images and converting them to a Texture. The raw image data can be keep in memory for further access.

后者可能与kwarg{}有关。从构造函数^{}

arg: can be a string (str), Texture, BytesIO or Image object

让我们检查以下四种可能性:

from kivy.app import App
from kivy.core.image import Image as Core_Image
from kivy.core.window import Window
from PIL import Image as PILImage
from io import BytesIO


class MyApp(App):

    def build(self):

        img_path = 'path/to/your/image.png'

        # From string path
        img = Core_Image(img_path)
        print(img.image)    # <kivy.core.image.img_sdl2.ImageLoaderSDL2 object at ...

        # From Texture object
        img = Core_Image(img.texture)
        print(img.image)    # None

        # From BytesIO object
        pil_img = PILImage.open(img_path)
        img_bytes = BytesIO()
        pil_img.save(img_bytes, format='PNG')
        img_bytes.seek(0)
        img = Core_Image(BytesIO(img_bytes.read()), ext='png')
        print(img.image)    # <kivy.core.image.img_sdl2.ImageLoaderSDL2 object at ...

        # From existing image object
        img = Core_Image(img_path)
        img = Core_Image(img)
        print(img.image)    # <kivy.core.image.img_sdl2.ImageLoaderSDL2 object at ...


myapp = MyApp().run()

当使用某些纹理对象作为源时,即使设置kwargkeep_data=True也不会在image属性中存储任何内容

现在,猜猜看^{}如何生成图像对象?正确:

img = Image(fbo.texture)

因此,重新阅读第二个链接,我们知道,所有内容都存储在相应的texture属性中!让我们看看(代码取自this Q&A):

from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock


class MyApp(App):

    def build(self):
        self.my_image = Image(source='path/to/your/image.png')
        Clock.schedule_once(self.export, 1)
        return self.my_image

    def export(self, dt):
        img = self.my_image.export_as_image()
        print(img.image)    # None
        print(img.texture)  # <Texture ... size=(800, 600) colorfmt='rgba' ...


myapp = MyApp().run()

在纹理对象的pixels属性中,存储原始数据。如果您想让它以某种方式“可读”,例如使用一些枕头Image对象,您需要转换:

from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
from PIL import Image as PILImage


class MyApp(App):

    def build(self):
        self.my_image = Image(source='path/to/your/image.png')
        Clock.schedule_once(self.export, 1)
        return self.my_image

    def export(self, dt):
        img = self.my_image.export_as_image()
        pil_img = PILImage.frombytes('RGBA',
                                     img.texture.size,
                                     img.texture.pixels)
        print(pil_img)    # <PIL.Image.Image image mode=RGBA size=800x600 at ...


myapp = MyApp().run()

现在,您应该能够在运行时从export_as_image的结果中访问单像素值

                    
System information
                    
Platform:      Windows-10-10.0.16299-SP0
Python:        3.9.1
PyCharm:       2021.1.1
kivy:          2.0.0
Pillow:        8.2.0
                    

相关问题 更多 >