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

(singke) #1
#Display HD Images to Screen #################
PrezScreen.fill(ScreenColor)
PrezScreen.blit(Picture,PictureLocation)
pygame.display.update()
pygame.time.delay(500)
#
# Quit with Mouse or Keyboard if Desired
for Event in pygame.event.get():
Command = "sudo umount " + PictureDisk
os.system(Command)
sys.exit()

This script works fine, but it runs slowly! Just getting the first picture to display can take a rather long
time.


By-the-Way: While You Test
While testing your presentation script, use small, simple, non-HD image files, such as
the .png files in the /home/pi/python_games directory. That way, you can get
everything working correctly without having to deal with the slow loading of HD files.
To do this, you just change the variable PictureDirectory to
/home/pi/python_games and the variable PictureFileExtension to
.png.

Unfortunately, when you load any HD image file, a Python script can really slow down. However,
there are a few things you can do to speed up the script as well as give the appearance of speed to
your presentation audience.


Improving the Presentation Speed


To improve the speed of the Python HD image presentation script, here are some modifications you
can make:


Load only functions used in modules instead of loading entire modules.
Remove any implemented delays.
Add buffering to the screen.
Do not convert images.
Add a title screen.
Add finer mouse and/or keyboard controls.

Each one of these changes might improve the speed of the presentation by only a second or even just a
millisecond. However, each little bit will help improve the flow of your HD image presentation. The
following sections describe how to implement these optimizations.


Loading Only Functions Instead of Entire Modules


Loading only the functions used will speed up any Python script. When you load an entire module
using the import statement, all the functions it contains are also loaded—and this can really slow
down a script. A good tip is to create a chart of your Python script that shows the modules imported
and the actual functions used. Table 23.2 shows how this might look. This table lists each module,
along with each of the functions used from that module. This type of chart will be helpful as you make

Free download pdf