Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Image in pygame?

Tags:

python

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)

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

like image 510
Rasovica Avatar asked Jul 09 '12 07:07

Rasovica


People also ask

Does pygame work with PNG?

Saving images only supports a limited set of formats. You can save to the following formats. New in pygame 1.8: Saving PNG and JPEG files.

How do I control an image in pygame?

To position an object on the screen, we need to tell the blit() function where to put the image. In pygame we always pass positions as an (X,Y) coordinate. This represents the number of pixels to the right, and the number of pixels down to place the image. The top-left corner of a Surface is coordinate (0, 0).


1 Answers

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()

BTW, you probably want to limit how much images you take per second. Either use time.sleep or a pygame 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
like image 109
sloth Avatar answered Oct 15 '22 13:10

sloth