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

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

关于python - 如何使用 Pygame 将捕获的图像保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20502237/

相关文章:

python - 如何在 Django 中获取查询集的所有子项?

python - 如何使用 Python Pandas ExcelWriter 将单元格颜色添加到单元格范围?

python - Pygame-子弹和敌人的碰撞,没有 Sprite 或类

python - Pygame 中的 2D 光线转换和矩形碰撞(视线、障碍物)

python - 为什么我会收到 TypeError : '<' not supported between instances of 'int' and 'tuple' ?

python - 如何检测 Pygame 中对象之间的碰撞?

python - 解决 n-queens 时发生奇怪的事情

python - 使用 Selenium 获取渲染页面的当前 HTML

将 OpenCV 从 3.0.0 降级到 2.4.11 时出现 Python cv2 链接问题

python - pygame platformer - 我怎样才能使底部坚固?