2012-07-09 90 views
9

我想从网络摄像头加载图像显示在pygame的 为什么会出现这种不work.Pygame窗口打开,但没有显示我使用videocapture如何在pygame中显示图像?

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) 

+0

是你的形象命名1.JPG并在同一文件夹? – 2012-07-09 07:23:24

+0

老兄,如果你看看你看到的代码saveSnapshot(str(In)+“。jpg”) – Rasovica 2012-07-10 14:27:34

+0

你可以使用'pygame.camera'来拍照并取消对'VideoCapture'的依赖关系 – steffen 2016-03-30 12:40:33

回答

13

你必须告诉pygame到update the display

在循环中添加以下行的图像块传输到屏幕后:

pygame.display.flip() 

顺便说一句,你可能想限制你每秒钟多少图像取。请使用time.sleeppygame clock


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 
+1

你应该'import pygame .time'而不只是'time'。其中一个(即我)可能会将它与Python库模块'time'混淆。此外:如果您以后决定导入库模块“time”,会发生什么情况? – steffen 2016-03-30 12:43:03