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

发布于 2024-12-20 12:11:35 字数 2137 浏览 1 评论 0原文

我正在尝试让我的网络摄像头通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2024-12-27 12:11:35

尝试创建一个像这样的相机。

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

这就是 pygame 文档的此页面上的完成方式。


查看 pygame.cameraAPI 页面,我发现有两件事可能会有所帮助。第一的,

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

实验!:此 api 可能会在以后的 pygame 中更改或消失
发布。如果你使用这个,你的代码很可能会破坏
下一个 pygame 版本。

当想知道为什么这会令人惊讶地失败时,请记住这一点。

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

从相机获取图像作为相机本机像素格式的字符串。对于与其他库集成很有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文