11

我想从网络摄像头加载图像以显示在 pygame 我正在使用 videocapture

from VideoCapture import Device
import pygame
import time
In=1
pygame.init()
w = 640
h = 480
size=(w,h)
screen = pygame.display.set_mode(size) 

while True:
    cam = Device()
    cam.saveSnapshot(str(In)+".jpg") 
    img=pygame.image.load(In)
    screen.blit(img,(0,0))
    In=int(In)+1
    In=str(In)

为什么这不起作用。Pygame 窗口打开但没有显示?

4

1 回答 1

17

你必须告诉 pygame更新 display

将图像传送到屏幕后,在循环中添加以下行:

pygame.display.flip()

顺便说一句,您可能想限制每秒拍摄的图像数量。使用time.sleeppygame 时钟


from VideoCapture import Device
import pygame
import time
In=1
pygame.init()
w = 640
h = 480
size=(w,h)
screen = pygame.display.set_mode(size) 
c = pygame.time.Clock() # create a clock object for timing

while True:
    cam = Device()
    filename = str(In)+".jpg" # ensure filename is correct
    cam.saveSnapshot(filename) 
    img=pygame.image.load(filename) 
    screen.blit(img,(0,0))
    pygame.display.flip() # update the display
    c.tick(3) # only three images per second
    In += 1
于 2012-07-09T07:37:50.323 回答