I want to be able to take a photo from a webcam in python 3 and Windows. Are there any modules that support it? I have tried pygame, but it is only linux and python 2, and VideoCapture is only python 2.
07/08/14
Pygame 3.4 Ver. is released
http://www.youtube.com/watch?v=SqmSpJfN7OE
http://www.lfd.uci.edu/~gohlke/pythonlibs/
You can download "pygame‑1.9.2a0.win32‑py3.4.exe"
take a photo from a webcam in python 3.4 (testing on window 7) code [1]
import pygame
import pygame.camera
pygame.camera.init()
cam = pygame.camera.Camera(0,(640,480))
cam.start()
img = cam.get_image()
pygame.image.save(img,"filename.jpg")
refer to
[1] Capturing a single image from my webcam in Java or Python
I've been looking for the same thing and so far I have come up with a blank. This is what I have so far:
2.7 3.2 3.3 3.4 LINUX WIN32 ------------------------------------------------------- OpenCV YES - - - YES YES PyGame YES YES YES YES YES YES SimpleCV YES - - - YES YES VideoCapture YES - - - - YES
Resources
- opencv.org/downloads.html
- pygame.info/downloads/
- simplecv.org/download
- videocapture.sourceforge.net/
07/08/14
Pygame 3.4 Ver. is released
http://www.youtube.com/watch?v=SqmSpJfN7OE
http://www.lfd.uci.edu/~gohlke/pythonlibs/
You can download "pygame‑1.9.2a0.win32‑py3.4.exe"
take a photo from a webcam in python 3.4 (testing on window 7) code [1]
import pygame
import pygame.camera
pygame.camera.init()
cam = pygame.camera.Camera(0,(640,480))
cam.start()
img = cam.get_image()
pygame.image.save(img,"filename.jpg")
refer to
[1] Capturing a single image from my webcam in Java or Python
import cv2
# Open the device at the ID 0
cap = cv2.VideoCapture(0)
#Check whether user selected camera is opened successfully.
if not (cap.isOpened()):
print("Could not open video device")
#To set the resolution
cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 480)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('preview',frame)
#Waits for a user input to quit the application
if cv2.waitKey(1) & 0xFF == ord('q'):
#break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Here's Pygame code that will take a photo from the webcam using Pygame. As of 2023, Pygame supports both Windows and Python 3.
One downside is that it can take about 0.6 or 0.7 seconds after calling the start()
method for the webcam to get ready. Before that, it just returns all-black images instead of photos. You can either add a 1 second pause before calling get_image()
to take a photo, or run code that checks if the returned image is all-black or not (I have this code in my example, but commented out.)
import pygame.camera, pygame.image, time
pygame.camera.init()
all_webcams = pygame.camera.list_cameras()
webcam = pygame.camera.Camera(all_webcams[0]) # Use the first found webcam.
webcam.start() # Initialize the webcam.
time.sleep(1) # Wait for the camera to get ready.
photo = webcam.get_image() # Take a photo from the webcam.
# Uncomment the following and remove the previous two lines to have
# the webcam keep taking photos until it finds one that isn't all black.
#while True:
# photo = webcam.get_image() # Take a photo from the webcam.
#
# # Check if the photo is all black because the webcam wasn't ready:
# all_black = True
# for x in range(photo.get_width()):
# for y in range(photo.get_height()):
# if photo.get_at((x, y)) != (0, 0, 0, 255):
# all_black = False
# break
# if not all_black:
# break # The webcam was ready and took a real photo.
# else:
# time.sleep(0.1) # Wait before trying to take another photo.
pygame.image.save(photo, "photo.png") # You can also use .jpg or .bmp.
pygame.camera.quit()
© 2022 - 2025 — McMap. All rights reserved.