Skip to content

Instantly share code, notes, and snippets.

@ereli
Last active June 13, 2018 21:50
Show Gist options
  • Save ereli/82b193c22124631f00ef3a5f3afe3c5a to your computer and use it in GitHub Desktop.
Save ereli/82b193c22124631f00ef3a5f3afe3c5a to your computer and use it in GitHub Desktop.
capture still images in python using pygame and fire
import time
from pathlib import Path
import pygame
import pygame.camera
from pygame.locals import *
import time
import fire
def show():
pygame.camera.init()
for i in pygame.camera.list_cameras():
print(i)
def init(folder):
folder_path = Path(folder).resolve()
try:
folder_path.mkdir(exist_ok=False)
except FileExistsError:
pass
def capture(camera,folder):
t = time.localtime()
folder_path = Path(folder).resolve()
init(folder)
pygame.camera.init()
try:
pygame.camera.list_cameras() #Camera detected or not
except:
raise("couldn't read camera")
cam = pygame.camera.Camera(camera,(1920,1080))
cam.start()
img = cam.get_image()
timestamp = time.strftime('%b-%d-%Y_%H%M', t)
out_file = str(folder_path.joinpath("img_{0}.jpg".format(timestamp)).absolute())
print("Saving filename: {0}".format(out_file))
pygame.image.save(img,out_file)
if __name__ == '__main__':
fire.Fire()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment