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/8461497/

相关文章:

python - 断言失败 : Flask server stops after script is run

python - 如何使用 Anaconda Python 3.6 在 64 位 Ubuntu 14.04 中安装 pygame

python - 在Python 3中使用pygame以特定分辨率播放电影

python - 这个特定的双 for 循环在 python 中是如何工作的?解释双 for 循环

python - SDL_Surface/LP_SDL_Surface 的 PySDL2 问题

python - 在 Python numpy 掩码数组中用最近的邻居填充缺失值?

python - 将 csv 转换为 json 多文档?

r - 绘制没有插值的 3D 表面?

3d - 切片 NURBS 曲面

python - 哪个Django模板布局更好?