如何在pygame中显示图像? [英] How to display Image in pygame?

查看:96
本文介绍了如何在pygame中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网络摄像头加载图像以在 pygame 上显示我正在使用视频捕捉

I want to load an image from webcam to display on pygame I am using videocapture

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 窗口打开但没有显示?

Why does this not work.Pygame window opens but nothing displays?

推荐答案

你必须告诉 pygame 更新显示.

You have to tell pygame to update the display.

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

Add the following line in your loop after blitting the image to the screen:

pygame.display.flip()

<小时>

顺便说一句,您可能想要限制每秒拍摄的图像数量.使用 time.sleeppygame时钟.

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

这篇关于如何在pygame中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆