0

i'm working with camera, with pygame, i wrote this code:

import pygame.camera
import pygame.image
pygame.camera.init()
list_webCam = pygame.camera.list_cameras()

webcam = pygame.camera.Camera(list_webCam[0],(640,480))
webcam.start()
img = webcam.get_image()
pygame.image.save(img, "photo.jpg")
pygame.camera.quit()

but it returns me this error:

Traceback (most recent call last):
  File "C:\Users\Fulvio\Desktop\pygame.Camera.py", line 11, in <module>
    pygame.image.save(img, "photo.jpg")
TypeError: must be pygame.Surface, not None
Fulvio Denza
  • 67
  • 1
  • 2
  • 11
  • 1
    Evidently `get_image` is returning `None`. Have you tried [`query_image`](http://www.pygame.org/docs/ref/camera.html#pygame.camera.Camera.query_image) to see if it's ready? – jonrsharpe Sep 07 '14 at 14:17

2 Answers2

0

Fors ome reason, you didn't get the expected image. This error means the "img" object is "None" - that is, your call to get_image didn't get the expected result.

As far as I can tell, you are making all the needed calls to get the camera roling, and grabbing an image - so, you'd better do some debugging in there, and check introspect what your webcam object is, and try to call the .get_image() method interactively, observing what it returns.

To do that, you may either paste the relevant lines directly in a Python console, or put the `import pdb; pdb.set_trace()" statements at the beginning, and proceed through using the Python Debugger. (For such a short snippet, you'd probably be better pasting/typing directly in the interpreter).

If everything is fine in your setup, it may be that the camera may need some "warm-up" time between the start and get_image calls. If using the interactive mode you get your image, try adding an arbitrary delay (say 200ms), after the start call (use pygame.time.delay for that)

Also, as far as I know, pygame can't encode images to "jpg" - you have to save then as "bmp" - or use some other library to handle the pygame.Surface object and save to other image formats.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
0

I just passed from python 3.5 to 2.7 and I was having the same problem you have. After a quick search I solved it: get_image want 1 parameter: the pygame suface! So, you can do this:

import pygame
import pygame.camera
from pygame.locals import *
pygame.camera.init()
cam = pygame.camera.Camera(0,(640,480),"RGB")
cam.start()
img = pygame.Surface((640,480))
cam.get_image(img)
pygame.image.save(img, "img.jpg")
cam.stop()

img = pygame.Surface((640,480))

cam.get_image(img)

I guess you don't need this anymore since it's passed more than 1 year but I hope it can help someone else.