środa, 23 lipca 2014

Batch upload to Picasa

The problem: upload photos to Picasa (trivial); 2) scale them if neccessary before upload; 3) if photos contains some EXIF tags (geotags in particular) copy these tags to Picasa as well.

To achieve the above I use: googleCL (upload), convert (from ImageMagick bundle for scaling) exiftool (for metadata extraction/manipulation).

Using convert the script below (jpgresize.sh) scale picture to 2048 pixels along longest side:


#!/bin/bash
# Scale pictures to 2048 pixels along longest side for upload to Picasa
# Photos below 2048 x 2048 pixels do not count towards storage limit
# (cf. https://support.google.com/picasa/answer/6558?hl=en )
#
PICASA_FREE_LIMIT=2048

while test $# -gt 0; do
case "$1" in
-o) shift; OUT_FILE="$1";;
-o*) OUT_FILE="`echo :$1 | sed 's/^:-o//'`";;
*) FILE="$1";;
esac
shift
done

if [ -z "$OUT_FILE" ] ; then
my_pic="${FILE%.*}_s.${FILE#*.}"
else
my_pic="$OUT_FILE";
fi

if [ -f "$FILE" ] ; then

echo "** converting $FILE to $my_pic ***"

SIZE="2048x"

my_pic_width=`exiftool -ImageWidth "$FILE" | awk '{print $NF}'`
my_pic_height=`exiftool -ImageHeight "$FILE" | awk '{print $NF}'`

if [[ ( -z "$my_pic_width" ) || ( -z "$my_pic_height" ) ]] ; then
echo "*** $FILE has 0 width and/or height ***"; exit ;
fi

## http://www.imagemagick.org/Usage/resize/#resize
if [[ ( "$my_pic_width" -gt "$PICASA_FREE_LIMIT" ) || \
( "$my_pic_height" -gt "$PICASA_FREE_LIMIT" ) ]] ; then
if [ "$my_pic_width" -gt "$my_pic_height" ] ; then
SIZE="${PICASA_FREE_LIMIT}x>"
echo "*** $FILE width: $my_pic_width ; converting to $SIZE"
convert "$FILE" -geometry $SIZE "$my_pic"
else
SIZE="x$PICASA_FREE_LIMIT>"
echo "*** $FILE height: $my_pic_height ; converting to $SIZE"
convert "$FILE" -geometry $SIZE "$my_pic"
fi
else
## File is too small copy the original:
echo "*** $FILE has $my_pic_width in width; COPYING"
cp "$FILE" "$my_pic"
fi
else
echo "*** FILE $FILE not found! ***"
fi

Upload one picture to picasa with 1photo2picasa.sh


#!/bin/bash
#
# Upload photo to Picasa with googleCL
# It is assumed the photo contains UserComment GPSLatitude GPSLongitude GPSAltitude
# Exif tags which are copied to Picasa (see below for more details)
#
# Default album title:
ALBUMTITLE="???"

echo "$0 -a AlbumTitle FILE-2-UPLOAD"

while test $# -gt 0; do
case "$1" in
-a) shift; ALBUMTITLE="$1";;
-a*) ALBUMTITLE="`echo :$1 | sed 's/^:-a//'`";;
*) FILE="$1";;
esac
shift
done

AUTHOR=`exiftool -S -Artist $FILE`

if [ -z "$AUTHOR" ] ; then
# It there is no Artist tag it is assumed photo was not tagged properly
echo "*** ERROR: $FILE lacks Artist EXIF tag"
exit;
else
## Some tags are edited:
TAGS=`exiftool -S -UserComment $FILE | awk '{ $1=""; for (i=1;i<=NF;i++) { if ($i ~ /http/) { $i=""}}; \
gsub (/, +/, ",", $0); gsub (/ +,/, ",", $0); gsub (/^ +| +$/, "", $0); print $0}'`
GPSLat=`exiftool -S -c '%+.6f' -GPSLatitude $FILE | awk '{ print $2}'`
GPSLon=`exiftool -S -c '%+.6f' -GPSLongitude $FILE | awk '{ print $2}'`
GPSAlt=`exiftool -GPSAltitude $FILE -S -c "%.1f" | awk '{ if ($0 ~ /Below/) { print -$2} else {print $2}}'`

PICASA_TAGS=""
## Concatenate all tags
if [ -n "$TAGS" ] ; then PICASA_TAGS="$TAGS"; fi
if [ -n "$GPSLat" ] ; then PICASA_TAGS="$PICASA_TAGS,geo:lat=$GPSLat"; fi
if [ -n "$GPSLon" ] ; then PICASA_TAGS="$PICASA_TAGS,geo:lon=$GPSLon"; fi
if [ -n "$GPSAlt" ] ; then PICASA_TAGS="$PICASA_TAGS,geo:alt=$GPSAlt"; fi

# Upload to picasa:
google picasa post --title "$ALBUMTITLE" --src="$FILE" --photo="$FILE" --tags="$PICASA_TAGS"
fi

Finally simple bash script upload2picassa.sh uses jpgresize.sh and 1photo2picasa.sh to upload all .jpg files from the current directory to picasa:


#!/bin/bash
# Upload all .jpg files (scaled tp 2048) to picasa
#
echo "Uploading all .jpg files to album: $albumtitle"

albumtitle=$1

if [ -z "$albumtitle" ] ; then
echo "Podaj ID albumu!"; exit 1
fi

for file in *.jpg; do

echo "Uploading $file to $albumtitle album..."

outfile="${file%.*}_s.${file#*.}"

jpgresize.sh -p -o $outfile $file && 1photo2picasa.sh -a $albumtitle $outfile
done

1 komentarz: