patternpythonMinor
Pygame Slideshow
Viewed 0 times
pygameslideshowstackoverflow
Problem
I have written a basic slideshow program that uses pygame to manage the display screen. It cycles through all pictures files in the directory and sub-directories. There is a user configuration section for the start directory, sub-directories to ignore, image file type, display time, full screen or window display and adj_height. I like the window display to see the title of the picture. On the Raspberry Pi, I changed the title font to size 18, color blue in Menu, Preferences, Appearance Setting. The window display has a height adjustment to compensate for menu and title bar as I could not find how to get maximum window height before making the window.
I would like a code review. My start program seems a little awkward. Any other points would be great.
```
#!/usr/bin/env python
"""
Picture Slideshow that uses pygame.
This slideshow program will loop continuous on the directory,
from user configuration or command line argument, and sub-
directories skipping thoses in the ignore list. It will display
pictures in regular size or reduce them if bigger than the
screen size. It will only display files with extension in the
configuration file list of my_image_file. To exit the program,
click the mouse in the window or exit button. The keyboard ESC
or q key will also exit the program, on ssh terminal use CTRL-C.
Only tested on the Raspberry Pi 3.
"""
import os
import sys
import pygame
from pygame.locals import FULLSCREEN
from colorama import Fore, Style
# User Configurations
display_time = 2.5 # Time in seconds
full_screen = False # False equal window
# full_screen = True
Dir = '/home/pi/Pictures'
# Directories to ignore
ignore = 'Junk', 'Trash', 'Paintings'
my_image_files = '.bmp', '.jpg', '.JPG', '.png', '.gif', '.tif'
Title = 'Picture Slideshow'
# Adjust height of window for menu and title bar.
adj_height = 76
# Setup Variables
os.environ['DISPLAY'] = ':0.0'
# grey = 211, 211, 211
grey = 128, 128, 128
black = 0, 0, 0
magenta = 100, 0, 100
I would like a code review. My start program seems a little awkward. Any other points would be great.
```
#!/usr/bin/env python
"""
Picture Slideshow that uses pygame.
This slideshow program will loop continuous on the directory,
from user configuration or command line argument, and sub-
directories skipping thoses in the ignore list. It will display
pictures in regular size or reduce them if bigger than the
screen size. It will only display files with extension in the
configuration file list of my_image_file. To exit the program,
click the mouse in the window or exit button. The keyboard ESC
or q key will also exit the program, on ssh terminal use CTRL-C.
Only tested on the Raspberry Pi 3.
"""
import os
import sys
import pygame
from pygame.locals import FULLSCREEN
from colorama import Fore, Style
# User Configurations
display_time = 2.5 # Time in seconds
full_screen = False # False equal window
# full_screen = True
Dir = '/home/pi/Pictures'
# Directories to ignore
ignore = 'Junk', 'Trash', 'Paintings'
my_image_files = '.bmp', '.jpg', '.JPG', '.png', '.gif', '.tif'
Title = 'Picture Slideshow'
# Adjust height of window for menu and title bar.
adj_height = 76
# Setup Variables
os.environ['DISPLAY'] = ':0.0'
# grey = 211, 211, 211
grey = 128, 128, 128
black = 0, 0, 0
magenta = 100, 0, 100
Solution
- Review
There's quite a lot of code here, so I'm going to look at just one part of it, namely the function
pic_files.-
This function has two tasks: first, it has to find files that might contain images, and second, it has to call the function
new_pic for each image. This makes it hard to test the code (how can you be sure that it found the right set of files?) and hard to reuse in other programs (because you'd probably have to change it to call some other function).It would be better to split these tasks into two functions, then you could reuse one of them. What you want is to have one function that generates the filenames, and another that calls
new_pic. See the revised code below for how this can be done.-
The function calls
os.walk twice, first to get the files:files = next(os.walk(Dir))[2]and second to get the directories:
dirs = next(os.walk(Dir))[1]It would be better to call it just once and remember the result. You can use tuple unpacking to avoid the
[2] and [1]:_, dirs, files = next(os.walk(dir))-
It would be a good idea if the list of ignored directories were an argument to the function. This would make the function easier to reuse, because you could pass different lists of ignored directories in different circumstances.
-
This function only uses the first result generated by
os.walk, and implements its own recursion over the directory tree. I guess that's because of the need to ignore some of the directories. But in fact os.walk already has built-in support for pruning the directory tree. The documentation says:When topdown is
True [which it is by default — GDR], the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search [or] impose a specific order of visiting.- Revised code
def iterfiles(top, ignored=()):
"""Generate files below the top directory in sorted order, ignoring
directories with names in the ignored iterable.
"""
ignored = set(ignored)
for root, dirs, files in os.walk(top):
for f in sorted(files):
yield os.path.join(root, f)
dirs[:] = sorted(set(dirs) - ignored)
def pic_files(top):
"""Call new_pic for each file below the top directory."""
for f in iterfiles(top, ignored=ignore):
new_pic(f)Code Snippets
files = next(os.walk(Dir))[2]dirs = next(os.walk(Dir))[1]_, dirs, files = next(os.walk(dir))def iterfiles(top, ignored=()):
"""Generate files below the top directory in sorted order, ignoring
directories with names in the ignored iterable.
"""
ignored = set(ignored)
for root, dirs, files in os.walk(top):
for f in sorted(files):
yield os.path.join(root, f)
dirs[:] = sorted(set(dirs) - ignored)
def pic_files(top):
"""Call new_pic for each file below the top directory."""
for f in iterfiles(top, ignored=ignore):
new_pic(f)Context
StackExchange Code Review Q#144024, answer score: 2
Revisions (0)
No revisions yet.