Task 3: Create Slide Show on Raspberry Pi

starter code to get location of images in a folder
http://ideone.com/H7jk5S (Links to an external site.)

 Array length   

   print(len(files))

How can we use this to select random image from directory?

Pygame: may only be able to run this on rapsberry pi

import pygame
import time

pygame.init()#this should be first line after imports

Mian loop of pygame:

while not crashed:
    for event in pygame.event.get():
          if event.type == pygame.QUIT:#allows you to safely quit program
               crashed = True

    time.sleep( 5 )#wait 5 seconds - use this in slideshow

   #program your code here

    pygame.display.update()#timers protocol
   clock.tick(60)

pygame.quit()
quit()

 

Display image to screen:

myImage = pygame.image.load("location of image file")#load image in memory
myImage = pygame.transform.scale(myImage, (display_width, display_height))#scale it
gameDisplay.blit(myImage, (x,y)) #display

   import pygame
from random import randint
import time
import os
import glob

pygame.init()

display_width = 800
display_height = 600
#landscape images are ideal

path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
print (dir_path)#prints the current directory page
files=glob.glob(dir_path+"/*.jpg")#find the files that are .jpg
for file in files:
    print(file)#print to screen to make sure it found them all

gameDisplay = pygame.display.set_mode((display_width,display_height))#set the display
pygame.display.set_caption('Image Slideshow')

black = (0,0,0)#color for the frame
white = (255,255,255)

clock = pygame.time.Clock()
crashed = False
#yImg = pygame.image.load('emma.jpg')#load an image in memory

def showImg(x,y,iname):
    myImage = pygame.image.load(iname)#load image in memory
    myImage = pygame.transform.scale(myImage, (display_width, display_height))#scale it
    gameDisplay.blit(myImage, (x,y)) #display

x =  (display_width * 0.01)
y = (display_height * 0.01)

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:#allows you to safely quit program
            crashed = True
    
    gameDisplay.fill(white)
    myRint = randint(0,(len(files)-1))#produce a random number between 0 and amount of images
    
    print (myRint)#print number to screen
    showImg(x,y,files[myRint])#pass image name to showImg method
    time.sleep( 5 )#wait 5 seconds
        
    pygame.display.update()#timers protocol
    clock.tick(60)

pygame.quit()
quit()