How the get-photos script works


This script finds new photos, resizes them and saves them to the /photos folder. The script is 'optional' i.e. you only need to use it if you want the Pi itself (rather than a Samba attched PC) to update the /photos folder. This means it doesn't do anything 'vital' for the operation of the photoframe It's main function is to generate thumbnail photos to the exact display size (if you let fbi do your resizing, you are asking for trouble). First, fbi often gets landscape photo's 'wrong', 'croping' them into a 'square' aspect ratio, although (oddly) 'portrait' photo's are always scaled correctly. More to the point, fbi will be rescaling each of the 3 'different' images 'on the fly', at 1 per second, which not only wastes power but also takes longer than the 1s 'allowed' for each to be displayed Finally, saving a copy of the 'full resolution' photo is pointless = it's 15-20x more efficient to save only the exact correct size (1200x1400 jpg's are about 3 a Mb, whilst 16Mpixel camera jpegs are about 5-6Mb each) The get-photo script uses ImageMagick 'convert' to generate (and save) a (correctly) pre-scaled thumbnial of each new photo to fit the display. Note. ImageMagick is installed (if needed) by the go-photoframe script before this script is launched

Actual operation

Version 1 only checks USB sticks (and not mapped Media server folders) and over-writes any file of the same name. Operation is as follows :- 1) Wait for a USB stick to be inserted 2) Turn on the Loading LED (pull some GPIO pin Lo) 3) Copy/resize all .jpg's in the 'root' to /photos/ 4) Turn off the Loading LED (release GPIO pin) 5) Wait until the USB stick is removed 6) Loop back to top Unused GPIO pins that could be used for the LED are : BCM17 (pin11), 27 (pin13) or 22 (pin15). Gnd can be found on pin9 and 3v3 on pin17. To activate the LED, the GPIO line will 'pull Lo', so we need 3v3 (pin17) to power the LED and thus BCM22 (pin15) is the 'nearest choice'. #!/bin/bash # get-photos.sh for Mk2 PhotoFrame, last updated 28 Sept. 2016 # GPIO setup (GPIO27 (hdr. pin 13) is used to pull LED lo when loading) # Start by clearing any current setting (gpio remains set after the script crashes) sudo sh -c 'echo "27" > /sys/class/gpio/unexport' sudo sh -c 'echo "27" > /sys/class/gpio/export' # Tri-state the pin (set it's direction to 'in') - to pull the pin low, we set it's direction 'low' sudo sh -c 'echo "in" > /sys/class/gpio/gpio27/direction' # # source photos are in root of USB stick, set mount point (only done on launch, so doesn't matter if we get an 'already exists' error) mkdir -p /usbroot # copy resize is to /photos/ folder (existing files of same name are overwritten) # loop forever checking for USB stick insert while [ true ]; do # 1) Wait for a USB stick to be inserted (fdisk lists devices, grep looks for sda1 found = true, so need ! (for not found) while ! fdisk -l | grep -q "sda1"; do sleep 1 done # OK, usb stick found, lets mount it (read only) mount /dev/sda1 /usbroot -o noatime # 2) Turn on the Loading LED (pull GPIO 27, pin13 Low sudo sh -c 'echo "low" > /sys/class/gpio/gpio27/direction' # 3) Make/save thumbnials of all .jpg / .JPG in the /usbroot/ to /photos/?.jpg for filename in $(ls -A1 /usbroot/*.jpg); do convert $filename -resize '1600x1200' /photos$filename done # some camera jpg's use upper case (.JPG) file extension for filename in $(ls -A1 /usbroot/*.JPG); do # use SED to substitute (s) JPG with jpg (/g means 'replace all occurances') outname="$(echo $filename | sed 's/JPG/jpg/g')" convert $filename -resize '1600x1200' /photos$outname done # all files copied, unmount the usb drive umount /dev/sda1 # 4) Turn off the Loading LED (set GPIO27, pin13 as input) sudo sh -c 'echo "in" > /sys/class/gpio/gpio27/direction' # 5) Wait until the USB stick is removed while fdisk -l | grep -q "sda1"; do sleep 1 done # 6) Loop back done Note. The LED is 'on' when photos are being copied from the stick (so user doesn't remove USB stick) The same LED is used as a 'feedback' (key click) indicator for the iR remote control. After creating any new script, don't forget to make it 'executable' :-) sudo chmod +x get-photo.sh