20

What's the simplest way in Ubuntu 11.10 to programmatically guide (either from Bash or Python) the user to capture a webcam photo of themselves?

I can launch a simple app like Cheese, but I don't see an easy way to immediately detect or retrieve the photo it captures. I can also access and record the webcam stream directly via OpenCV, but I'd have to reinvent the GUI to communicate with the user.

Is there any kind of script that's a happy medium, where I can launch it, and it prints on stdout the filename of the image the user took?

Cerin
  • 60,957
  • 96
  • 316
  • 522

2 Answers2

39

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.

Community
  • 1
  • 1
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • 2
    By now, pygame can save to png, jpg, tiff and bmp. At least tose were the ones I tested. Just change the extension and pygame detects it. – steffen Mar 30 '16 at 12:32
  • pygame.camera.quit() doesn't work; use cam.stop() instead. – ingroxd Sep 27 '17 at 16:31
16

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.

Bogdan Mart
  • 460
  • 8
  • 19
Multimedia Mike
  • 12,660
  • 5
  • 46
  • 62
  • Link for The Pygame library is wrong now. The requested URL /docs/tut/camera/CameraIntro.html was not found on the server. – Andark Dec 08 '16 at 15:44