taking webcam photos in python 3 and windows
Asked Answered
S

4

6

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.

Strand answered 19/4, 2014 at 20:59 Comment(1)
As of 2023, Pygame's camera module does work on Windows and Python 3.Conspiracy
C
5

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

Chicanery answered 8/7, 2014 at 6:30 Comment(2)
When I tried, I got an <code>ImportError: cannot import name '_camera'</code>Ridglee
if "SystemError: ioctl(VIDIOC_S_FMT) failure: no supported formats ...." happens, try to include: cam.stop() after the last line. This should solve the problem when you are trying to take more than one photo.Cissy
S
7

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/
Superpose answered 17/6, 2014 at 20:12 Comment(0)
C
5

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

Chicanery answered 8/7, 2014 at 6:30 Comment(2)
When I tried, I got an <code>ImportError: cannot import name '_camera'</code>Ridglee
if "SystemError: ioctl(VIDIOC_S_FMT) failure: no supported formats ...." happens, try to include: cam.stop() after the last line. This should solve the problem when you are trying to take more than one photo.Cissy
S
0
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() 
Skiffle answered 20/4, 2022 at 19:11 Comment(1)
Welcome to Stack Overflow. Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. This is especially important when answering old questions (this one is 8 years old) with existing answers. See How to Answer.Propagable
C
0

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()
Conspiracy answered 6/9, 2023 at 1:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.