How to programmatically capture a webcam photo

自作多情 提交于 2019-12-02 15:56:31
Multimedia Mike

If you want to do this via Python, it looks like you have a few options. The Pygame library has the ability to access cameras.

If that's unsatisfactory, you can go much lower level and access the Video 4 Linux 2 API directly using ioctl calls using Python's fcntl library.

jsbueno

I like using pygame for that - it does not require you to open a Pygame SDL window, unlike when you want to use it to capture keyboard events, for example.

import pygame.camera
pygame.camera.init()
cam = pygame.camera.Camera(pygame.camera.list_cameras()[0])
cam.start()
img = cam.get_image()
import pygame.image
pygame.image.save(img, "photo.bmp")
pygame.camera.quit()

Though Pygame will only save uncompressed "bmp" files - you may want to combine it with PIL to write to other formats.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!