Skip to content

Instantly share code, notes, and snippets.

@waveform80
Last active April 19, 2017 07:00
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save waveform80/263b9c8bdcb1e9b79749 to your computer and use it in GitHub Desktop.
Custom output for writing individual JPEGs from an MJPEG
import io
import picamera
class MyOutput(object):
def __init__(self):
self.file_num = 0
self.output = None
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
if self.output:
self.output.close()
self.file_num += 1
self.output = io.open('%d.jpg' % self.file_num, 'wb')
self.output.write(buf)
with picamera.PiCamera() as camera:
camera.resolution = (1280, 720)
camera.start_recording(MyOutput(), format='mjpeg')
camera.wait_recording(10)
camera.stop_recording()
import io
import picamera
import curses
class MyOutput(object):
def __init__(self):
self.file_num = 0
self.write_frame = False
self.output = None
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
if self.output:
self.output.close()
self.output = None
if self.write_frame:
self.file_num += 1
self.output = io.open('%d.jpg' % self.file_num, 'wb')
self.write_frame = False
if self.output:
self.output.write(buf)
def main(window):
window.nodelay(True)
with picamera.PiCamera(resolution=(1280, 720), framerate=15) as camera:
output = MyOutput()
camera.start_recording(output, format='mjpeg')
try:
while True:
window.addstr(0, 0, 'Press Q to quit')
window.addstr(2, 0, 'Press F to capture a frame')
window.addstr(3, 0, 'Filename: %d.jpg' % output.file_num)
window.refresh()
c = window.getch()
if c == ord('q'):
break
elif c == ord('f'):
output.write_frame = True
camera.wait_recording(0.1)
finally:
camera.stop_recording()
curses.initscr()
curses.wrapper(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment