【问题标题】:How to display Image in pygame?如何在pygame中显示图像?
【发布时间】:2012-07-09 07:21:14
【问题描述】:

我想从网络摄像头加载图像以在 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.jpg 并且在同一个文件夹中?
  • 老兄,如果您查看代码,您会看到 saveSnapshot(str(In)+".jpg")
  • 可以使用pygame.camera拍照,去掉对VideoCapture的依赖

标签: python pygame


【解决方案1】:

你必须告诉 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多