gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:38 25 4
gpt4 key购买 nike

我正在尝试让我的网络摄像头通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com