-
-
Save palcode/7a567354ea2e87195b03efe376b20a59 to your computer and use it in GitHub Desktop.
Custom output for writing individual JPEGs from an MJPEG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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