如何在pygame中显示图像?

时间:2012-07-09 07:21:14

标签: python pygame

我想从网络摄像头加载图像以在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)

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

1 个答案:

答案 0 :(得分:14)

你必须告诉pygame update the display

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

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