繁体   English   中英

python pygame blit。 获取图像显示

[英]python pygame blit. Getting an image to display

我正在尝试让我的网络摄像头通过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))部分得到了一个错误

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

我认为这是因为我没有将图像转换为pygame可用的任何格式,但我不知道。

任何帮助将不胜感激。

-Alex

好的,所以这里是新代码:该代码有效,因为它将图片保存到当前文件夹。 弄清楚了最后一个为什么工作。 屏幕仍然是黑色,尽管= \\

# 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()

尝试创建一个像这样的相机。

cam = pygame.camera.Camera(camlist[0],(640,480))

这就是在pygame文档的此页面上完成的操作。


pygame.cameraAPI页面上,我发现有两点可能有所帮助。 第一,

Pygame当前仅支持Linux和v4l2摄像机。

实验!:此api在以后的pygame版本中可能会更改或消失。 如果使用此功能,则您的代码很可能会在下一个pygame版本中中断。

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

另外,您可以尝试调用camera.get_raw()并打印结果。 它应该是带有原始图像数据的字符串。 如果您得到一个空字符串, None或一些无意义的文本:请在此处与我们分享。 它会告诉您相机是否有东西。

从摄像机获取的图像作为字符串以摄像机的本机像素格式显示。 与其他库集成很有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM