| 1 |
#!/usr/bin/env python2 |
| 2 |
|
| 3 |
# Select random wallpaper from given directory or directories |
| 4 |
# Written by: Maciej Delmanowski <[email protected]> |
| 5 |
# Derived from: pjbackground |
| 6 |
# Usage: <wallpaper-program> `fvwm-crystal.wallpaper <directory> [directory] ... |
| 7 |
|
| 8 |
import sys, os, string, random |
| 9 |
|
| 10 |
# This function looks for image files in specified directory and it's |
| 11 |
# subdirectories and returns the list of absolute names |
| 12 |
def find_images(root): |
| 13 |
for path, dirs, files in os.walk(root): |
| 14 |
for filename in files: |
| 15 |
extension = os.path.splitext(filename) |
| 16 |
if string.lower(extension[1]) in ('.jpg','.jpeg','.png'): |
| 17 |
yield os.path.join(path, filename) |
| 18 |
|
| 19 |
# Exit with no arguments |
| 20 |
if len(sys.argv) == 1: |
| 21 |
sys.exit() |
| 22 |
|
| 23 |
# Find images in all directories specified in the command line |
| 24 |
images=[] |
| 25 |
|
| 26 |
for argument in sys.argv: |
| 27 |
for image in find_images(argument): |
| 28 |
images.append(image) |
| 29 |
|
| 30 |
# Select random wallpaper and print it |
| 31 |
print random.choice(images) |
| 32 |
|