Python 如何使用Pygame将捕获的图像保存到磁盘

Python 如何使用Pygame将捕获的图像保存到磁盘,python,pygame,Python,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 i

这是启动网络摄像头的代码:

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曲面。因此只需调用pygame.image.save(),文件类型由扩展名决定,默认为TGA。选项有BMP、TGA、PNG和JPEG。在这种情况下,您可以将此行附加到文件中

pygame.image.save(img, "image.jpg")
退房