- Pygame 教程
- Pygame - 首頁
- Pygame - 概述
- Pygame - Hello World
- Pygame - 顯示模式
- Pygame - Locals 模組
- Pygame - 顏色物件
- Pygame - 事件物件
- Pygame - 鍵盤事件
- Pygame - 滑鼠事件
- Pygame - 繪製形狀
- Pygame - 載入影像
- Pygame - 在視窗中顯示文字
- Pygame - 移動影像
- Pygame - 使用數字鍵盤移動
- Pygame - 使用滑鼠移動
- Pygame - 移動矩形物件
- Pygame - 使用文字作為按鈕
- Pygame - 影像變換
- Pygame - 音效物件
- Pygame - 混音器通道
- Pygame - 播放音樂
- Pygame - 播放影片
- Pygame - 使用攝像頭模組
- Pygame - 載入游標
- Pygame - 訪問 CDROM
- Pygame - Sprite 模組
- Pygame - PyOpenGL
- Pygame - 錯誤和異常
- Pygame 有用資源
- Pygame - 快速指南
- Pygame - 有用資源
- Pygame - 討論
Pygame - 使用攝像頭模組
Pygame 1.9.6 及之前的版本包含 pygame.camera 模組。此模組包含在遊戲視窗上捕獲攝像頭饋送並從中抓取影像的功能。系統可用的攝像頭裝置列在 list_cameras() 方法返回的列表中。
pygame.camera.list_cameras()
要初始化攝像頭物件,請使用攝像頭 ID、解析度和格式引數。
pygame.camera.Camera(device, (width, height), format)
預設格式為 RGB。寬度和高度引數預設為 640x480。
攝像頭模組在 Camera 類中定義了以下方法。
| pygame.camera.Camera.start() | 開啟、初始化並開始捕獲 |
| pygame.camera.Camera.stop() | 停止、取消初始化並關閉攝像頭 |
| pygame.camera.Camera.get_controls() | 獲取使用者控制元件的當前值 |
| pygame.camera.Camera.set_controls() | 如果攝像頭支援,則更改攝像頭設定 |
| pygame.camera.Camera.get_size() | 返回正在記錄的影像的尺寸 |
| pygame.camera.Camera.query_image() | 檢查幀是否已準備好 |
| pygame.camera.Camera.get_image() | 捕獲影像作為 Surface |
| pygame.camera.Camera.get_raw() | 將未修改的影像作為字串返回 |
示例
以下程式捕獲計算機預設網路攝像頭的即時饋送。
import pygame
import pygame.camera
pygame.init()
gameDisplay = pygame.display.set_mode((640,480))
pygame.camera.init()
print (pygame.camera.list_cameras())
cam = pygame.camera.Camera(0)
cam.start()
while True:
img = cam.get_image()
gameDisplay.blit(img,(0,0))
pygame.display.update()
for event in pygame.event.get() :
if event.type == pygame.QUIT :
cam.stop()
pygame.quit()
exit()
請注意,在 Windows 作業系統上,您可能需要安裝 Videocapture 模組。
pip3 install VideoCapture
輸出
廣告