Python 如何在pygame中显示图像?
我想从网络摄像头加载一个图像,显示在pygame上 我正在使用视频捕获Python 如何在pygame中显示图像?,python,pygame,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")
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窗口打开,但什么也不显示?您必须告诉Pygame 将图像点显到屏幕后,在循环中添加以下行:
pygame.display.flip()
顺便说一句,你可能想限制每秒拍摄的图像数量。使用
time.sleep
或
你的图像名为1.jpg并且在同一个文件夹中吗?伙计,如果你看代码,你会看到saveSnapshot(str(in)+“.jpg”),你可以使用
pygame.camera
拍摄照片,并消除对视频捕获的依赖
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