Hi, I´m using a Raspeberry Pi with camera and kivy. The camera allows me to capture an image to a stream. The picamera docs then shows how to load that into a PIL Image, as follows:
import io
import time
import picamera
from PIL import Image as PILImage
# Create the in-memory stream
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.start_preview()
time.sleep(2)
camera.capture(stream, format='jpeg')
# "Rewind" the stream to the beginning so we can read its content
stream.seek(0)
image = PILImage.open(stream)
I want to load that image to an foto(Image) widget, without having to write it to memory, I've tried:
self.cam.capture(self.stream,'jpeg')
self.cam.close()
self.stream.seek(0)
self.PILim= PILImage.open(self.stream)
self.tex = Texture.create(size=(1240,980), colorfmt='rgb')
self.tex.blit_buffer(self.PILim.tostring())
self.foto.texture = self.tex
self.stream.seek(0)
self.foto.reload()
I've also tried to load it directly from the stream, without using a PIL object, doing something like
self.foto.source = self.stream
Getting an 'source only accepts string as source' error. I want to take advantage of not having to write the 'jpg' file to the sdcard, I guess it would be much faster if the image is already in RAM
I forgot to mention., none of this worked
import io
import picamera as p
from kivy.graphics.texture import Texture
from kivy.graphics import Rectangle
from kivy.uix.image import Image
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class testApp(App):
def build(self):
self.b= BoxLayout()
self.image_composition = Image()
self.cam = p.PiCamera()
self.cam.resolution = 1600,896
self.stream=io.BytesIO()
self.cam.capture(self.stream, 'rgb')
self.cam.close()
self.stream.seek(0)
self.tex = Texture.create(size=(1600,896), colorfmt='rgb')
self.tex.blit_buffer(self.stream.getvalue(), colorfmt='rgb', bufferfmt='ubyte')
with self.image_composition.canvas:
Rectangle(texture = self.tex, size = (1600,896))
print 'final'
self.b.add_widget(self.image_composition)
return self.b
testApp().run()
Cache.remove('kv.texture')