I am using Debian Linux and Python 2.7 with PIL. I want to capture an image from a webcam and store it in a file. What are the ways in which this can be done? Also,I want to covert the captured image for modification using Scipy. What is the procedure.for this. My code is intended to run on the raspberry Pi.

3 Years
Discussion Span
Last Post by Gribouillis

I've done this sort of stuff before. To answer your second question, here's a code sub-snippet:

import copy
import Image
import numpy as np
img  = Image.open('E:\\JCH\\Snap.jpg')
newx = img.size[0]
newy = img.size[1]      # Image is (newx,newy) pixels

imga = np.asarray(img)      # This is the secret; convert img to array
imgc = copy.copy(imga)      # Make a copy, lest you step on toes

# then the value of imgc[y,x] is the pixel [R,G,B] or [R,G,B,A]
# Note the order [y,x] for the pixel at (x,y). You're welcome :-)

Saving a camera snapshot is fairly easy if you download VideoCapture (videocapture.sourceforge.net):

>> from VideoCapture import Device
>> cam = Device()
>> cam.saveSnapshot('Snap.jpg')

Edited by BearofNH: Add pointer for VideoCapture


@BearofNH Are you sure it works on linux and with an ordinary webcam


Oops, you're right. VideoCapture is Win32 only. For Linux, break out your favorite search engine. I had no trouble finding the command:

uvccapture -d /dev/video0 -o outfile.jpg

... although if you want something more Pythonic I suggest opencv. At least TRY the above command. If it works then you've probably got all the Linux infrastructure you need.

Votes + Comments
very interesting

This is very interesting BearofNH. Unfortunately it does not work with all hardware (eg my laptop :( ). I'm trying to port this code to python, using the python bindings for v4L2 and opencv2. If it works, I'll make a code snippet. See also the author's blog.

Edited by Gribouillis

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.