1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
# camera: take a picture, animate it on screen
import gi
gi.require_version("Gst", "1.0")
from gi.repository import Gst
Gst.init(None)
import pippy
import pygame
import sys
import time
# grey background
bgcolor = (128, 128, 128)
# start using pygame
pygame.init()
# turn off cursor
pygame.mouse.set_visible(False)
# create the pygame window and return a Surface object for
# drawing in that window.
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# grab a frame from camera to file
pipeline = Gst.parse_launch('v4l2src ! videoconvert ! jpegenc ! filesink location=/tmp/pippypic.jpg')
pipeline.set_state(Gst.State.PLAYING)
# keep trying to load in the grabbed camera frame until it works
while True:
try:
image = pygame.image.load('/tmp/pippypic.jpg')
break
except pygame.error:
time.sleep(1)
# stop the camera frame grabbing
pipeline.set_state(Gst.State.NULL)
# set initial rotation angle and scale
angle = 0.0
scale = 2.0
while pippy.pygame.next_frame():
# every time we animate, check for quit or keydown events and exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
sys.exit()
# rotate and scale the image
newImage = pygame.transform.rotozoom(image, angle, scale)
newImageRect = newImage.get_rect()
newImageRect.centerx = screen.get_rect().centerx
newImageRect.centery = screen.get_rect().centery
# display the rotated and scaled image
screen.fill(bgcolor)
screen.blit(newImage, newImageRect)
pygame.display.flip()
# choose a new rotation angle and scale
angle = angle + 5.0
scale = scale * 0.95
# finish once the scale becomes very very small
if scale < 0.001:
break
|