LivingThePiLife
Posts: 11
Joined: Fri Aug 10, 2018 3:43 pm

Stream picamera preview to tkinter

Wed May 15, 2019 9:09 pm

Hi
I’m looking for a way to stream picamera in a live view to a tkinter frame. I’ve tried a couple of ideas I found on the net but can’t get anything working.
Picamera preview isn’t viewable over VNC so I need to set up a stream I think.
The code below is a mock up from my program and it’s the closest I can get to it actually working but I’d like it live stream. When you click the camera button the live view will pop up where you can edit the camera settings before its passed to a time lapse camera. Runs in python 2
Thanks

Code: Select all

#!/usr/bin/env python
from picamera import PiCamera
from Tkinter import *
from PIL import Image,ImageFont, ImageDraw
from PIL import ImageTk
from io import BytesIO

stream = BytesIO()
camera = PiCamera()
splashScreenFont = ImageFont.truetype('/usr/share/fonts/dejavu/DejaVuSans.ttf',15)
img= Image.new('RGBA',(200,200),(120,20,20))
draw = ImageDraw.Draw(img)
draw.text((0,0),'   Arghhhhhhhhhhhhhhhh\n      Doing my head in',(255,255,0),font=splashScreenFont)
draw = ImageDraw.Draw(img)
img.save('startup.jpg')
  
class streaming:
  def __init__(self, master):
    self.master = master
    self.master.geometry('+300+100')
    self.master.title("Streaming Test")
    self.master.resizable(width=False, height=False)
    self.path="startup.jpg"
    self.img = Image.open(self.path)
    self.img= self.img.resize((400,400), Image.ANTIALIAS)
    self.img = ImageTk.PhotoImage(self.img)
    self.imgFrame= Label(master,image=self.img,height=400,width=400)
    self.imgFrame.grid(row=0,column=0, columnspan=5, rowspan=15,padx=5)
    self.cameraButton=Button(master, text='Camera',command=self.CameraButton)
    self.cameraButton.grid(row=16,column=0, sticky=E+W)
 
  def CameraButton(self):
    if self.cameraButton.config('relief')[-1]=='sunken':
      self.cameraButton.config(relief='raised')
      camera.stop_preview()
    else:
      self.cameraButton.config(relief='sunken')
      camera.resolution=(380,380)
      camera.preview_fullscreen=False
      camera.preview_window=(500,270,150,150)
      camera.start_preview()
      camera.capture(stream, format='jpeg')
      image=Image.open(stream)
      image= image.resize((400,400), Image.ANTIALIAS)
      image = ImageTk.PhotoImage(image)
      self.imgFrame.configure(image=image)
      self.imgFrame.image =image

root = Tk()
my_gui = streaming(root)
root.mainloop()

Return to “Camera board”