Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

Avoiding Converting Images


In Hour 19, you learned that for games, it is wise to use the .convert_alpha() operation to load
images. This way, game images can be converted once to speed up the operation of the game. The
opposite is true here because these pictures will be displayed to the screen only one time. To make
this modification, you need to make the image.load function for each picture look as follows.


Click here to view code image


Picture = image.load(Picture)

Remember that this statement was previously written like this:


Click here to view code image


Picture = pygame.image.load(Picture).convert_alpha()

You remove pygame here because you are no longer loading the entire pygame module. You
remove .convert_alpha() to improve the speed of the image loading.


Making this change improves the speed of image loading by about 3 to 5 seconds. Making this change
also gives you the exact pixel format displayed on the screen that is in the image file. So you get two
improvements in one!


Even with these two improvements to image.load, this particular command will still be the
slowest one in the entire script due to the large size of HD image files. However, many performance
enhancements are in the works for the Raspberry Pi, so this may not be a problem in the future.


Watch Out!: Preloading Images
Since loading images takes so long, it would make sense to preload the images into a
Python list before you begin displaying them to the screen. However, if you try to do
this, you will most likely run out of memory. It would work, though, if you had non-HD
images or just a few pictures. But trying to load up several HD photos will cause your
Python script to run out of memory, suddenly quit, and leave you with the message
“Killed” displayed on the screen.

Adding a Title Screen


The next best thing to do to improve the speed of the presentation script is to give the audience the
illusion of speed.


Sending text to the presentation screen happens relatively quickly. Thus, by adding a title screen at the
beginning of the script, you can give that first picture time to load. Also, this will prevent your
audience from staring at a blank screen for 20 to 30 seconds.


To incorporate a title screen, you set up some variables and text to be used within the presentation, as
follows:


Click here to view code image


# Set up Presentation Text #######################
#
# Color #
#
RazPiRed = 210,40,82
#
Free download pdf