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

Vip*_*pul 7 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
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()
Run Code Online (Sandbox Code Playgroud)

我想知道如何捕获图像并将其存储到当前目录.请提出所需的更改建议.

Aro*_*tti 10

当您调用webcam.get_image时,它将返回RGB Surface.所以只需调用pygame.image.save(),文件类型由扩展名确定,默认为TGA.选项包括BMP,TGA,PNG和JPEG.在这种情况下,您可以将此行附加到您的文件中.

pygame.image.save(img, "image.jpg")
Run Code Online (Sandbox Code Playgroud)

查看http://www.pygame.org/docs/ref/image.htmlhttp://www.pygame.org/docs/ref/camera.html