python pygame blit。获取要显示的图像

python pygame blit。获取要显示的图像,python,pygame,geometry-surface,blit,Python,Pygame,Geometry Surface,Blit,我正试图让我的摄像头通过pygame显示视频。代码如下: # import the relevant libraries import time import pygame import pygame.camera from pygame.locals import * # this is where one sets how long the script # sleeps for, between frames.sleeptime__in_seconds = 0.05 # initialis

我正试图让我的摄像头通过pygame显示视频。代码如下:

# import the relevant libraries
import time
import pygame
import pygame.camera
from pygame.locals import *
# this is where one sets how long the script
# sleeps for, between frames.sleeptime__in_seconds = 0.05
# initialise the display window
pygame.init()
pygame.camera.init()

screen = pygame.display.set_mode((640, 480), 0, 32)

# set up a camera object
cam = pygame.camera.Camera(0)
# start the camera
cam.start()

while 1:

    # sleep between every frame
    time.sleep( 10 )
    # fetch the camera image
    image = cam.get_image()
    # blank out the screen
    screen.fill((0,0,2))
    # copy the camera image to the screen
    screen.blit( image, ( 0, 0 ) )
    # update the screen to show the latest screen image
    pygame.display.update()
当我尝试这个方法时,我从screen.blit(image,(0,0))部分得到一个错误


尝试创建一个这样的相机

cam=pygame.camera.camera(camlist[0],(640480))
这就是在pygame文档上的操作方式


查看for
pygame.camera
,我发现了两件可能有用的事情。首先,

Pygame目前仅支持Linux和v4l2摄像头

实验性的!:此api可能会在以后的pygame中更改或消失 释放。如果使用此选项,您的代码很可能会与 下一个pygame版本

当想知道为什么这会出人意料地失败时,请记住这一点

在一个更高节奏的音符上。。。您可以尝试调用
camera.get_raw()
并打印结果。它应该是一个包含原始图像数据的字符串。如果您得到一个空字符串,
None
,或一些无意义文本:请在此处与我们共享。它会告诉你是否从摄像机里得到了什么

以相机的本机像素格式的字符串形式从相机获取图像。有助于与其他库集成


cam.get\u image()
正在返回
None
。该错误意味着图像为None,而不是它是错误格式的有效图像。显然,cam.get_image()由于某些原因失败。您可能需要调整图片分辨率,具体取决于您的相机。我的相机不是这样的。这就是为什么我用另一种方式做的,那部分很好。只是必须将图像放到屏幕上的部分不起作用,不管怎样,
get_image
都会自动失败。所以之前的一些方法不起作用。好吧,那么我将尝试另一种方法。我找到了一个代码,它可以拍摄图像并保存下来,并且可以正常工作。所以我要尝试合并它们。马上回来
Traceback (most recent call last):
  File "C:\Python32\src\webcam.py", line 28, in <module>
    screen.blit( image, ( 0, 0 ) )
TypeError: argument 1 must be pygame.Surface, not None
# import the relevant libraries
import time
import pygame
import pygame.camera
from pygame.locals import *
# this is where one sets how long the script
# sleeps for, between frames.sleeptime__in_seconds = 0.05
# initialise the display window
pygame.init()
pygame.camera.init()
# set up a camera object
size = (640,480)
screen = pygame.display.set_mode(size,0)


surface = pygame.surface.Surface(size,0,screen)

cam = pygame.camera.Camera(0,size)
# start the camera
cam.start()

while 1:

    # sleep between every frame
    time.sleep( 10 )
    # fetch the camera image
    pic = cam.get_image(surface)
    # blank out the screen
    #screen.fill((0,0,0))
    # copy the camera image to the screen
    screen.blit(pic,(0,0))
    # update the screen to show the latest screen image
    p=("outimage.jpg")

    pygame.image.save(surface,p)
    pygame.display.update()