如何使用 Pygame 将捕获的图像保存到磁盘 [英] How to save captured image to disk, using Pygame

查看:68
本文介绍了如何使用 Pygame 将捕获的图像保存到磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my code, which starts the webcam :

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()

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

I want to know how to capture an image and store it to the current directory. Please suggest the changes required.

推荐答案

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

When you call webcam.get_image it returns an RGB Surface. So just call pygame.image.save(), the file type is determined by the extension and defaults to TGA. Options are BMP, TGA, PNG, and JPEG. In this case you could append this line to your file.

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

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

这篇关于如何使用 Pygame 将捕获的图像保存到磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆