python pygame 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
# 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 的图像,但我不知道。

如有任何帮助,我们将不胜感激。谢谢。

-亚历克斯

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

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

这就是在 this page 上完成的方式pygame 文档。


查看 API page对于 pygame.camera,我发现了两件事可能会有帮助。首先,

Pygame currently supports only Linux and v4l2 cameras.

EXPERIMENTAL!: This api may change or disappear in later pygame releases. If you use this, your code will very likely break with the next pygame release.

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

更乐观一点...您可以尝试调用 camera.get_raw() 并打印结果。它应该是带有原始图像数据的字符串。如果您收到空字符串、None 或一些无意义的文本:请在此处与我们分享。它会告诉您是否从相机中获取了任何信息。

Gets an image from a camera as a string in the native pixelformat of the camera. Useful for integration with other libraries.

关于python pygame blit。获取要显示的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16203973/

相关文章:

python - PyGame:将透明度应用于具有 alpha 的图像?

python - 锯齿 tkinter 主循环帧持续时间?

python - 更改时间列精度

python - 添加数据框并根据可用性划分结果

python - url 调度程序使用正则表达式检测所有内容或不检测任何内容

python - Pygame 分发 - 运行时错误

当在屏幕中添加外星人时,Python Pygame 船移动缓慢

python - 是否必须对新的 pygame 表面进行转换?

java - 获取形成圆周的点的坐标

python - pygame的安装