#!/usr/bin/python3
# ===================================================================
# Automated Picture Frame (incomplete)
#
# For more information see:
#
# note.nkmk.me/en/python-pillow-basic/
# ===================================================================
import re
import os
import user_interface as ui
from PIL import Image
PICDIR = '/home/tom/Pictures/'
FILEPATS = [ '\.jpg$', '\.png$' ]
# -------------------------------------------------------------------
# ---- test if a directory exists - returns True or False
# -------------------------------------------------------------------
def dir_exists(dir):
if os.path.isdir(dir):
return True
return False
# -------------------------------------------------------------------
# ---- test if a file matched a regexp - return True or False
# ---- (ignore case)
# -------------------------------------------------------------------
def file_match(file,patterns):
for p in patterns:
if re.search(p,file,re.IGNORECASE):
return True
return False
# -------------------------------------------------------------------
# ---- get a list files in a directory that match a regexp
# -------------------------------------------------------------------
def get_list_of_files(dir,pats):
# --- get a list of entries in the directory
flist = os.listdir(dir)
# --- get a list of audio files
files = [] # list of files
for f in flist: # for each file name
ff = dir + f # file path + name
if os.path.isfile(ff): # regular file?
if file_match(f,pats): # file name match pattern?
files.append(f) # save file name
return files
# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------
if __name__ == '__main__':
# ---- running Python3?
if not ui.running_python3():
print()
print('Must run Python3 - exit program')
print()
sys.exit()
# ---- fix path to picture files (must end in '/')
if not re.search('/$',PICDIR):
PICDIR = PICDIR + '/'
# ---- does the audio file directory exists
if not dir_exists(PICDIR):
print()
print(f'Directory {PICDIR} does not exist')
sys.exit()
# ---- get a list of picture files
piclist = get_list_of_files(PICDIR,FILEPATS)
if len(piclist) < 1:
print()
print('No pictures found - program exit')
print()
sys.exit()
# ---- menu
while(True):
ui.clear_screen()
print('=====================================================')
print('===================== My Pictures ===================')
print('=====================================================')
i = 0
for p in piclist:
print(f'[{i:3}] {p}')
i += 1
print()
s = ui.get_user_input('Select picture: ')
if not s: # empty string?
break
x,n = ui.is_int(s)
if not x:
print()
print(f'You entered a bad selection - try again')
ui.pause()
continue
if n < 0 or n > len(piclist):
print()
print('Bad selection - try again')
print()
ui.pause()
continue
pic = PICDIR + piclist[n]
im = Image.open(pic)
im.show()
ui.pause()
print()