python pygame blit.获取要显示的图像 [英] python pygame blit. Getting an image to display

查看:119
本文介绍了python pygame blit.获取要显示的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的网络摄像头通过 pygame 显示视频.代码如下:

# 导入相关库导入时间导入pygame导入 pygame.camera从 pygame.locals 导入 *# 这是设置脚本多长的地方# 在帧之间休眠.sleeptime__in_seconds = 0.05# 初始化显示窗口pygame.init()pygame.camera.init()屏幕 = pygame.display.set_mode((640, 480), 0, 32)# 设置相机对象cam = pygame.camera.Camera(0)# 启动相机cam.start()而 1:# 在每一帧之间休眠time.sleep( 10 )# 获取相机图像图像 = cam.get_image()# 清空屏幕screen.fill((0,0,2))# 将相机图像复制到屏幕上screen.blit( 图像, ( 0, 0 ) )# 更新屏幕以显示最新的屏幕图像pygame.display.update()

当我尝试这个时,我从 screen.blit( image, ( 0, 0 ) ) 部分得到一个错误

回溯(最近一次调用最后一次): 中的文件C:\Python32\src\webcam.py",第 28 行screen.blit( 图像, ( 0, 0 ) )类型错误:参数 1 必须是 pygame.Surface,而不是 None

我认为这是因为我没有将图像转换为任何适用于 pygame 的图像,但我不知道.

任何帮助将不胜感激.谢谢.

-亚历克斯

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

# 导入相关库导入时间导入pygame导入 pygame.camera从 pygame.locals 导入 *# 这是设置脚本多长的地方# 在帧之间休眠.sleeptime__in_seconds = 0.05# 初始化显示窗口pygame.init()pygame.camera.init()# 设置相机对象大小 = (640,480)屏幕 = pygame.display.set_mode(size,0)表面 = pygame.surface.Surface(size,0,screen)cam = pygame.camera.Camera(0,size)# 启动相机cam.start()而 1:# 在每一帧之间休眠time.sleep( 10 )# 获取相机图像pic = cam.get_image(surface)# 清空屏幕#screen.fill((0,0,0))# 将相机图像复制到屏幕上screen.blit(pic,(0,0))# 更新屏幕以显示最新的屏幕图像p=("outimage.jpg")pygame.image.save(表面,p)pygame.display.update()

解决方案

尝试创建这样的相机.

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

在 pygame 文档的此页面上就是这样做的.><小时>

查看 pygame.cameraAPI 页面,我发现两件事可能会有所帮助.首先,

<块引用>

Pygame 目前仅支持 Linux 和 v4l2 相机.

EXPERIMENTAL!: 这个 api 可能会在以后的 pygame 中改变或消失发布.如果您使用它,您的代码很可能会与下一个 pygame 版本.

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

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

<块引用>

从相机获取图像作为相机原生像素格式的字符串.用于与其他库集成.

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

解决方案

Try creating a Camera like this.

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

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,

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.

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.

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.获取要显示的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆