python pygame 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 的任何内容,但我不这样做知道。
任何帮助将不胜感激。谢谢。
-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()
I'm trying to get my webcam to show video through pygame. Here is the code:
# 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()
When i try this I get an error from the screen.blit( image, ( 0, 0 ) ) part
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
I assume It's because I didn't convert the image into whatever works with pygame, but i don't know.
Any help would be appreciated.Thanks.
-Alex
OK so here is the new code:
This one works because it saves the picture to the current folder. figured out why the last one work. the screen is still black although=\
# 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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试创建一个像这样的相机。
这就是 pygame 文档的此页面上的完成方式。
查看
pygame.camera
的 API 页面,我发现有两件事可能会有所帮助。第一的,当想知道为什么这会令人惊讶地失败时,请记住这一点。
更乐观的是......您可以尝试调用camera.get_raw()并打印结果。它应该是一个包含原始图像数据的字符串。如果您收到空字符串、
None
或一些无意义的文本:请在此处与我们分享。它会告诉你是否从相机中得到了任何东西。Try creating a Camera like this.
That's how it's done on this page of the pygame docs.
Looking at the API page for
pygame.camera
, I found two things that might help. First,Keep that in mind when wondering why this surprisingly fails.
On a more up-beat note... you can try calling
camera.get_raw()
and printing the result. It should be a string with raw image data. If you get an empty string,None
, or some non-sense text: please share that with us here. It'll tell if you're getting anything from the camera.