2012-07-09 110 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