Display PIL Image on Kivy Canvas

2019-07-11 04:18发布

I can not find any documentation on how to display a PIL Image on a Kivy Canvas.

Rectangle(source=image) give TypeError: 'Image' object has no attribute '__getitem__'

I need to use a pillow image due to other image manipulations. I even tried to load the image into a texture then apply the texture to the canvas but then I can't figure out how to convert the formats into something compatible. The Kivy documentation on how to load images on both textures and canvases is pretty much non-existent.

2条回答
Ridiculous、
2楼-- · 2019-07-11 04:38
from kivy.core.image import Image as CoreImage
from kivy.uix.image import Image as kiImage
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO

canvas_img = Image.new('RGB', (240, 120), color=(255, 255, 255))
(do stuff to canvas_img)
data = BytesIO()
canvas_img.save(data, format='png')
data.seek(0) # yes you actually need this
im = CoreImage(BytesIO(data.read()), ext='png')
self.beeld = kiImage() # only use this line in first code instance
self.beeld.texture = im.texture
查看更多
够拽才男人
3楼-- · 2019-07-11 04:41

In the following example, it demonstrates using PIL Image to manipulate images and present it as source, texture and canvas of Kivy Image widgets.

Example

main.py

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.image import CoreImage
from kivy.lang import Builder

from PIL import Image

kv = Builder.load_string('''
#:kivy 1.11.0

<RootWidget>:
    img: img
    img3: img3
    img4: img4
    do_default_tab: False

    TabbedPanelItem:
        text: 'PIL Image'

        Screen:
            RelativeLayout:
                Image:
                    id: img
                    pos_hint: {"left": 1, 'bottom': 1}
                    size_hint: 0.5, 1
                    allow_stretch: True

            RelativeLayout:
                Image:
                    id: img3
                    pos_hint: {"right": 1, 'bottom': 1}
                    size_hint: 0.5, 1
                    allow_stretch: True

    TabbedPanelItem:
        text: 'canvas'

        Screen:
            FloatLayout:
                Image:
                    id: img4
                    keep_data: True
                    allow_stretch: True
                    canvas.before:
                        Color:
                            rgba: 0, 0, 0, 1  # Black
                        Rectangle:
                            pos: self.pos
                            size: self.size


''')


class RootWidget(TabbedPanel):

    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        iw = Image.open("./DSC08518.JPG")   # Use PIL.Image
        iw.save('./phase.jpg')
        gray = iw.convert('1')
        gray.save('./gray_im.jpg')
        self.img.source = './phase.jpg'
        self.img3.texture = CoreImage('./gray_im.jpg').texture
        self.img4.source = './gray_im.jpg'


class KivyPILApp(App):
    title = "Kivy & PIL Demo"

    def build(self):
        return RootWidget()


if __name__ == "__main__":
    KivyPILApp().run()

Output

Img01 - Kivy Image Source & Texture Img02 - Kivy Image canvas

查看更多
登录 后发表回答