to be used for your HD images.
Finding the Images
Now that you have your presentation screen set up, you need to find your images, so you need to know
their file names. Once again, the idea is flexibility: You want to be able to add or delete photo files
without needing to edit the Python script.
To have your script find your images for you, put two variables in the script that describe where the
pictures are located and their file extensions. In this example, the variable PictureDirectory is
set to point to the location of the stored photographs:
Click here to view code image
PictureDirectory = '/home/pi/pictures'
PictureFileExtension = '.jpg'
The variable PictureFileExtension is set to the photo’s current file extension. If you have
multiple file extensions, you can easily add additional variables, such as
PictureFileExtension1.
After you set the location of the photos and their file extensions, list the files in that photo directory.
To accomplish this, you need to import another module, the os module, as shown here:
Click here to view code image
import os #Import OS module
You can use the os.listdir operation to list the contents of the directory that contains the photos.
Each file located in the directory will be captured by the Picture variable. Thus, you can use a
for loop to process each file as it is encountered:
Click here to view code image
for Picture in os.listdir(PictureDirectory):
You may have other files located in this directory, especially if you use a removable drive. (See the
section “Storing Photos on a Removable Drive,” later in this hour.) To grab and display only the
desired images, you can have the script check each file for the appropriate file extension before
loading it with the .endswith operation. You can use an if statement to conduct this operation.
The for loop should now look as follows:
Click here to view code image
for Picture in os.listdir(PictureDirectory):
if Picture.endswith(PictureFileExtension):
Now, all the pictures ending with the appropriate file extension will be processed. The rest of the
loop simply loads each picture and then fills the screen with the designated screen color:
Click here to view code image
Picture=PictureDirectory + '/' + Picture
Picture=pygame.image.load(Picture).convert_alpha()
PictureLocation=Picture.get_rect() #Current location
#Display HD Images to Screen #################3
PrezScreen.fill(ScreenColor)
PrezScreen.blit(Picture,PictureLocation)
pygame.display.update()