简体   繁体   English

python pygame blit。 获取图像显示

[英]python pygame blit. Getting an image to display

I'm trying to get my webcam to show video through pygame. 我正在尝试让我的网络摄像头通过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 当我尝试这个时,我从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

I assume It's because I didn't convert the image into whatever works with pygame, but i don't know. 我认为这是因为我没有将图像转换为pygame可用的任何格式,但我不知道。

Any help would be appreciated.Thanks. 任何帮助将不胜感激。

-Alex -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. 这就是在pygame文档的此页面上完成的操作。


Looking at the API page for pygame.camera , I found two things that might help. pygame.cameraAPI页面上,我发现有两点可能有所帮助。 First, 第一,

Pygame currently supports only Linux and v4l2 cameras. Pygame当前仅支持Linux和v4l2摄像机。

EXPERIMENTAL!: This api may change or disappear in later pygame releases. 实验!:此api在以后的pygame版本中可能会更改或消失。 If you use this, your code will very likely break with the next pygame release. 如果使用此功能,则您的代码很可能会在下一个pygame版本中中断。

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. 另外,您可以尝试调用camera.get_raw()并打印结果。 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. 如果您得到一个空字符串, None或一些无意义的文本:请在此处与我们分享。 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. 与其他库集成很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM