9

How can I get a single image from a camera (e.g. a built-in webcam in a notebook) with Python?

I'm looking for a library that makes this possible in a few lines of code (I'm not looking to write a camera driver or anything like that). The image should be in some standard bitmap format.

Please also

  • list requirements (e.g. installed packages, operating system),
  • add a link to the documentation
  • This question has been migrated to Software Recommendations. I have edited your title to be more on-topic here, and removed two requirements that you cannot reasonably expect to be fulfilled here. You can revert the edit if you disagree. – user416 Jan 12 '16 at 8:15
  • For all answers, please note stackoverflow.com/a/34687991/562769 – Martin Thoma Jan 12 '16 at 9:01
  • Please tell us your operating system: linux, mac, windows ... – guettli Jun 1 '16 at 14:42
6

SimpleCV

Installation

On Debian-based systems:

sudo apt-get install python-opencv

Code

import time
from SimpleCV import Camera

cam = Camera()
time.sleep(0.1)  # If you don't wait, the image will be dark
img = cam.getImage()
img.save("simplecv.png")

SimpleCV image

enter image description here

Although the lighting situation did not change, the Python image is much darker. I'm not sure why that is the case.

  • My first guess is that the python image is scaled from 0-1 whereas it should be 0-255. – Dirk Feb 9 '15 at 20:47
  • @Dirk: Do you have an idea how to fix it? – Martin Thoma Feb 9 '15 at 20:54
  • Multiply by 255, then save it as png :) – Dirk Feb 9 '15 at 21:09
  • @Dirk: This seems not to be the problem. I printed a single pixel and it showed the RGBA value with integers. So I guess a 0..255 range is used. However, I am not sure why it was RGBA and not RGB. – Martin Thoma Feb 9 '15 at 21:57
  • 3
    I've just filed an issue Camera image is very dark for SimpleCV. Lets see if they know how to fix it. – Martin Thoma Feb 10 '15 at 15:31
4

OpenCV

Installation

On Debian-based systems:

sudo apt-get install python-opencv

Code

import time
import cv2
camera_port = 0
camera = cv2.VideoCapture(camera_port)
time.sleep(0.1)  # If you don't wait, the image will be dark
return_value, image = camera.read()
cv2.imwrite("opencv.png", image)
del(camera)  # so that others can use the camera as soon as possible
  • @PadraicCunningham Thanks, I've added it. Do you know if the other have / need something similar? – Martin Thoma Feb 9 '15 at 22:03
  • cam.stop() in pygame – Padraic Cunningham Feb 9 '15 at 22:10
2

i just modified the @Martin Thoma Code.

import cv2
camera = cv2.VideoCapture(0)
while True:
    return_value,image = camera.read()
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    cv2.imshow('image',gray)
    if cv2.waitKey(1)& 0xFF == ord('s'):
        cv2.imwrite('test.jpg',image)
        break
camera.release()
cv2.destroyAllWindows()

to resolve the black screen problem. i hope it'll be help. thank you.

2

PyGame

This answer is partially taken from https://stackoverflow.com/a/11094891/562769 - thanks Froyo!

Thanks to Phani for helping me to improve it.

  • Requirements:
    • Packages: pygame (not in PyPI)
    • Python 2.X (not tested with 3 though)
    • OS: Works on Ubuntu
  • Documentation: http://www.pygame.org/docs/

Installation

On Debian-based systems:

sudo apt-get install python-pygame

Code

import pygame
import pygame.camera
import time

pygame.camera.init()
pygame.camera.list_cameras()
cam = pygame.camera.Camera("/dev/video0", (640, 480))
cam.start()
time.sleep(0.1)  # You might need something higher in the beginning
img = cam.get_image()
pygame.image.save(img, "pygame.jpg")
cam.stop()
  • It will be useful to add a time.sleep(0.1) here as well. Otherwise, the images are dark – Phani Sep 15 '16 at 20:38
  • 1
    The sleep has to be after cam.start(). Also, for me 0.1 is not enough on my Dell laptop. 0.5 worked for me. – Phani Sep 16 '16 at 21:49
  • @Phani Very interesting. I tested it a couple of times and I guess 0.1 was to low for my computer, too. However, after running my script a couple of times I don't need the "sleep" line of code. The last thing I tried was putting the sleep between cam = ... and cam.start(). I (wrongly) assumed that it has to be there, because it worked after changing that. – Martin Thoma Sep 17 '16 at 8:14
  • On windows 7, py36, I get ImportError: cannot import name '_camera'. – nu everest May 21 '17 at 20:33
1

ecapture

Installation

pip install ecapture 

Code

from ecapture import ecapture as ec

ec.capture(0,"test","img.jpg")
0

I would cut this problem into two pieces.

  1. Take a picture from the command line

I hope you use an operating system like linux which has a command line.
AFAIK gphoto2 can do something like this

  1. Use the Python subprocess module

    subprocess.call(['yourtoo', 'arg1, ...])

-3

To solve the black screen problem, let the cam warms up.

import time 
import cv2
camera_port = 0
camera = cv2.VideoCapture(camera_port)
time.sleep(0.1)
return_value, image = camera.read()
cv2.imwrite("opencv.jpg", image)
del(camera)  # so that others can use the camera as soon as possible
  • 1
    Which is exactly what I wrote in the comment above 2 months ago. If you think it should be an answer, you should at least make it a community wiki answer. – Martin Thoma Mar 1 '16 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.