display pil image on kivy canvas 1

display pil image on kivy canvas

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

Here is what the above code is Doing:
1. Create a new PIL image, canvas_img, with a size of 240×120 and a white background.
2. Do stuff to canvas_img.
3. Create a BytesIO object, data, to hold the image data.
4. Save the image to the BytesIO object.
5. Seek to the beginning of the BytesIO object.
6. Create a CoreImage object, im, from the BytesIO object.
7. Create a Kivy Image object, self.beeld, from the CoreImage object.

Similar Posts