0
votes

I'm using the Python script example from the Raspberry PiCamera site here to display a 'lower third' graphic over the video camera display. Then I plan to capture that HDMI video (HDMI to USB adapter) and use it during video meetings (Zoom, Google Meet, etc.).

What I want to do is to have the script update the image without restarting the script. It could just be every couple seconds or it could watch the file and when it changes, reload it.

Here's the script as it stands now. I'm not a Python expert so I thought I would come here. Thanks!

import picamera

from PIL import Image
from time import sleep

with picamera.PiCamera() as camera:
  camera.start_preview()
  img = Image.open('lol.gif')
  pad = Image.new('RGBA', (
      ((img.size[0] + 31) // 32) * 32,
      ((img.size[1] + 15) // 16) * 16,
      ))
  pad.paste(img, (0, 0), img)
  o = camera.add_overlay(pad.tostring(), size=img.size)
  o.alpha = 255
  o.layer = 3

while True:
    sleep(1)

1

1 Answers

0
votes

Ok, I worked out the code to watch a file's modification date and reload it it has changed. It may not be perfect but it works. It was located in /home/pi/camera along with the lower third graphics. Which all were 1920x1080 PNG files with alpha channels.

import picamera
import os.path, time

from PIL import Image
from time import sleep

filename = "/home/pi/camera/lower-third.png"

with picamera.PiCamera() as camera:

  camera.start_preview()
  img = Image.open(filename)
  pad = Image.new('RGBA', (
      ((img.size[0] + 31) // 32) * 32,
      ((img.size[1] + 15) // 16) * 16,
      ))
  pad.paste(img, (0, 0), img)
  o = camera.add_overlay(pad.tobytes(), size=img.size)
  o.alpha = 255
  o.layer = 3
  ft = time.ctime(os.path.getmtime(filename))

  while True:
    sleep(1)
    nft = time.ctime(os.path.getmtime(filename))
    if nft != ft:
      ft = nft
      img = Image.open(filename)
      pad = Image.new('RGBA', (
          ((img.size[0] + 31) // 32) * 32,
          ((img.size[1] + 15) // 16) * 16,
          ))
      pad.paste(img, (0, 0), img)
      camera.remove_overlay(o)
      o = camera.add_overlay(pad.tobytes(), size=img.size)
      o.alpha = 255
      o.layer = 3