Quick answer: Camera hardware needs warm-up time. Skip first 5–10 frames after start(). Verify OS permission before access.
A photo-mode game uses Pygame Camera module. First few frames are black. Webcam exposure hasn’t adjusted; sensor still in standby.
Warm-Up Skip
cam = pygame.camera.Camera("/dev/video0", (640, 480))
cam.start()
for _ in range(10): # discard warm-up frames
if cam.query_image():
cam.get_image()
pygame.time.delay(50)
Polls but discards. After ~500ms, image is properly exposed.
OS Permission
- macOS: requires Camera privacy permission. Cold start triggers OS prompt; user must grant.
- Windows: Settings → Privacy → Camera → Allow apps. Some antivirus blocks camera too.
- Linux: user must be in ‘video’ group.
Show a UI hint “Please allow camera access” if start() fails.
Pixel Format Compatibility
cam = pygame.camera.Camera(device, (640, 480), "RGB")
RGB is most portable. If your camera doesn’t support it, get YUV and convert.
Handle Camera Disconnect
if not cam.query_image():
# still warming up OR disconnected
pass
If query_image returns False repeatedly, the camera might have been unplugged. Re-init or fall back to static fallback image.
Verifying
Start camera. First frames discarded; visible frames show correctly exposed image. Quit and restart; same behavior.
“Hardware needs to wake. Discard warm-up frames; permission errors graceful.”
For game capture/photo modes, consider the OpenCV alternative — better cross-platform support and richer format options than pygame.camera.