Python Webcam Capture Code Snippet

By | September 8, 2011

Here is a small piece of code how to query your webcam and save the frame as an image.

import sys
import Image, ImageDraw, datetime
import opencv
#this is important for capturing/displaying images
from opencv import highgui

camera = highgui.cvCreateCameraCapture(0)
def get_image():
    for i in range(10):
            im = highgui.cvQueryFrame(camera)
    # Add the line below if you need it (Ubuntu 8.04+)
    #im = opencv.cvGetMat(im)
    #convert Ipl image to PIL image
    return opencv.adaptors.Ipl2PIL(im)

im = get_image()
draw = ImageDraw.Draw(im)
now =
draw.text((10,10), str(now))'/var/www/webcam/img/home.png', "PNG")

It’s quite easy to hook it up to a website to take a snap anytime.

One thought on “Python Webcam Capture Code Snippet

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.