![](/img/trans.png)
[英]How to save pygame Surface as an image to memory (and not to disk)
[英]How to save captured image to disk, using Pygame
这是我的代码,它启动网络摄像头:
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
cameras = pygame.camera.list_cameras()
print "Using camera %s ..." % cameras[0]
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
pygame.display.flip()
# grab next frame
img = webcam.get_image()
我想知道如何捕获图像并将其存储到当前目录。 请提出所需的更改建议。
当您调用webcam.get_image时,它将返回RGB Surface。 所以只需调用pygame.image.save(),文件类型由扩展名确定,默认为TGA。 选项包括BMP,TGA,PNG和JPEG。 在这种情况下,您可以将此行附加到您的文件中。
pygame.image.save(img, "image.jpg")
查看http://www.pygame.org/docs/ref/image.html和http://www.pygame.org/docs/ref/camera.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.