drive a webcam with python
Sunday, September 17th, 2006I bought a USB webcam off of eBay quite some time ago, and I decided to connect it to my telescope with a little bit of hardware hackery. I’ll have to see about posting a writeup on how I did that at a later time. Anyway, when I installed my camera software, I quickly found how horrible the program was. It gave a tiny preview of what the camera saw, and had no way of capturing images or video without waaaay too many clicks of the mouse. That’s when I decided to write my own in Python.
The main libraries that I ended up using were VideoCapture, PIL, and pygame. As you’ll see, it’s not really difficult at all to make a useful webcam application.
Here’s the code:
from VideoCapture import Device import ImageDraw, sys, pygame, time from pygame.locals import * from PIL import ImageEnhance res = (640,480) pygame.init() cam = Device() cam.setResolution(res[0],res[1]) screen = pygame.display.set_mode((640,480)) pygame.display.set_caption('Webcam') pygame.font.init() font = pygame.font.SysFont("Courier",11) def disp(phrase,loc): s = font.render(phrase, True, (200,200,200)) sh = font.render(phrase, True, (50,50,50)) screen.blit(sh, (loc[0]+1,loc[1]+1)) screen.blit(s, loc) brightness = 1.0 contrast = 1.0 shots = 0 while 1: camshot = ImageEnhance.Brightness(cam.getImage()).enhance(brightness) camshot = ImageEnhance.Contrast(camshot).enhance(contrast) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() keyinput = pygame.key.get_pressed() if keyinput[K_1]: brightness -= .1 if keyinput[K_2]: brightness += .1 if keyinput[K_3]: contrast -= .1 if keyinput[K_4]: contrast += .1 if keyinput[K_q]: cam.displayCapturePinProperties() if keyinput[K_w]: cam.displayCaptureFilterProperties() if keyinput[K_s]: filename = str(time.time()) + ".jpg" cam.saveSnapshot(filename, quality=80, timestamp=0) shots += 1 camshot = pygame.image.frombuffer(camshot.tostring(), res, "RGB") screen.blit(camshot, (0,0)) disp("S:" + str(shots), (10,4)) disp("B:" + str(brightness), (10,16)) disp("C:" + str(contrast), (10,28)) pygame.display.flip()
I decided to use pygame in order to build this because it can actually handle the fps that I need for video. I built something similar quite a while ago with wxPython, but it just wasn’t fast enough at image swapping to make it work well.
A couple of noteworthy points:
- The function on line 15 is simply there to help automate displaying information on the screen. In my case, I display the number of screenshots I’ve saved to disk, and the current brightness and contrast levels. The function displays the text sent to it by writing it in dark gray, and then re-writing it in light gray over the top with a pixel offset. I found this helps a TON to make the text visible in different scenarios.
- Line 26 is where I actually apply the contrast and brightness changes. Lines 30-33 set up the number keys 1-4 to adjust contrast/brightness on the fly. This is terribly useful, in my experience.
- Lines 34 and 35 are setting up ‘q’ and ‘w’ to show the settings dialogs for the webcam… this is where you can set resolutions, exposure levels, etc.
- On line 36, and those under the if statement, I save the current image and name it using the current time… this insures no overlapping image names.
If you’re trying to write a webcam app of your own, I hope this gets you pointed in the right direction. Let me know if you find this info useful!