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?
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.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With