简体   繁体   English

TypeError:必须是pygame.Surface,而不是None

[英]TypeError: must be pygame.Surface, not None

i'm working with camera, with pygame, i wrote this code: 我正在使用相机和pygame,我编写了以下代码:

import pygame.camera
import pygame.image
pygame.camera.init()
list_webCam = pygame.camera.list_cameras()

webcam = pygame.camera.Camera(list_webCam[0],(640,480))
webcam.start()
img = webcam.get_image()
pygame.image.save(img, "photo.jpg")
pygame.camera.quit()

but it returns me this error: 但是它返回了这个错误:

Traceback (most recent call last):
  File "C:\Users\Fulvio\Desktop\pygame.Camera.py", line 11, in <module>
    pygame.image.save(img, "photo.jpg")
TypeError: must be pygame.Surface, not None

Fors ome reason, you didn't get the expected image. 由于某些原因,您没有获得预期的图像。 This error means the "img" object is "None" - that is, your call to get_image didn't get the expected result. 此错误表示“ img”对象为“ None”-也就是说,您对get_image的调用未获得预期的结果。

As far as I can tell, you are making all the needed calls to get the camera roling, and grabbing an image - so, you'd better do some debugging in there, and check introspect what your webcam object is, and try to call the .get_image() method interactively, observing what it returns. 据我所知,您正在进行所有必要的调用以使相机旋转并获取图像-因此,最好在其中进行一些调试,并检查一下自己的网络摄像头对象,然后尝试调用.get_image()方法以交互方式观察它返回的内容。

To do that, you may either paste the relevant lines directly in a Python console, or put the `import pdb; 为此,您可以将相关的行直接粘贴到Python控制台中,也可以将“ import pdb; pdb.set_trace()" statements at the beginning, and proceed through using the Python Debugger . (For such a short snippet, you'd probably be better pasting/typing directly in the interpreter). 首先是pdb.set_trace()“语句,然后继续使用Python Debugger 。(对于这么短的代码段,您最好直接在解释器中粘贴/键入)。

If everything is fine in your setup, it may be that the camera may need some "warm-up" time between the start and get_image calls. 如果您的设置一切正常,则可能是在start调用和get_image调用之间,摄像机可能需要一些“热身”时间。 If using the interactive mode you get your image, try adding an arbitrary delay (say 200ms), after the start call (use pygame.time.delay for that) 如果使用交互式模式获取图像,请尝试在start调用之后添加任意延迟(例如200ms)( pygame.time.delay使用pygame.time.delay

Also, as far as I know, pygame can't encode images to "jpg" - you have to save then as "bmp" - or use some other library to handle the pygame.Surface object and save to other image formats. 而且,据我所知,pygame无法将图像编码为“ jpg”-您必须另存为“ bmp”-或使用其他库来处理pygame.Surface对象并保存为其他图像格式。

I just passed from python 3.5 to 2.7 and I was having the same problem you have. 我刚从python 3.5传递到2.7,遇到了同样的问题。 After a quick search I solved it: get_image want 1 parameter: the pygame suface! 快速搜索后,我解决了这个问题:get_image要1个参数:pygame界面! So, you can do this: 因此,您可以这样做:

import pygame
import pygame.camera
from pygame.locals import *
pygame.camera.init()
cam = pygame.camera.Camera(0,(640,480),"RGB")
cam.start()
img = pygame.Surface((640,480))
cam.get_image(img)
pygame.image.save(img, "img.jpg")
cam.stop()

img = pygame.Surface((640,480)) img = pygame.Surface((640,480))

cam.get_image(img) cam.get_image(img)

I guess you don't need this anymore since it's passed more than 1 year but I hope it can help someone else. 我想您已经超过1年了,所以您不再需要它了,但我希望它可以帮助其他人。

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

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