better display of description and time for a single photo
[photo-tags.git] / create_thumbnails.sh
1 #!/bin/bash
2
3 #    copyright 2012,2013 Arun Persaud <arun@nubati.net>
4 #
5 #    This file is part of photo-tags.
6 #
7 #    Photo-tags is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    Photo-tags is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with Photo-tags.  If not, see <http://www.gnu.org/licenses/>.
19
20 NICE="nice -19"
21
22 # init some variables
23 removemeta=0
24
25 # parse ini file
26 CONFIG_FILE="config.ini"
27
28 # copied and modified from http://mark.aufflick.com/blog/2007/11/08/parsing-ini-files-with-sed
29 eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
30     -e 's/;.*$//' \
31     -e 's/\[.*\]//' \
32     -e 's/[[:space:]]*$//' \
33     -e 's/^[[:space:]]*//' \
34     -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
35    < $CONFIG_FILE `
36
37 LOCALDB=$fspotdb
38 dbprefix=${dbprefix//\//\\\/}
39 limit=`$NICE sqlite3 $LOCALDB "select count(*) from photos"`
40
41
42 function usage()
43 {
44   echo "Usage: $0 [OPTIONS]"
45   echo " Options:"
46   echo "          -r          remove metadata from pictures"
47   echo "          -l NUMBER   only process NUMBER pictures"
48 }
49
50 # parse command line
51 while getopts ":rl:h" opt; do
52   case $opt in
53     r)
54       removemeta=1
55       ;;
56     l)
57       limit=$OPTARG
58       ;;
59     h)
60       usage
61       exit 0
62       ;;
63     \?)
64       echo "Invalid option: -$OPTARG" >&2
65       exit 1
66       ;;
67     :)
68       echo "Option -$OPTARG requires an argument." >&2
69       exit 1
70       ;;
71   esac
72 done
73
74
75 #make sure we are in the correct directory
76
77 #make sure we can access the database
78 if [ ! -s $LOCALDB ] ; then
79     echo "Can't find database...exiting."
80     exit 1
81 fi
82
83 #create directories, iff they don't exist
84 if [ ! -d "Photos-small" ] ; then
85     echo "creating local directory to store photos of reduced size"
86     mkdir Photos-small
87 fi
88
89 if [ ! -d "Photos-tiny" ] ; then
90     echo "creating local directory to store thumbnails"
91     mkdir Photos-tiny
92 fi
93
94 done=0
95 offset=0
96 FILES=""
97
98 while [ $done -lt $limit ] ; do
99    echo "skipping $offset pics, getting $limit new pics to work on..."
100    # handle white space in filename correctly
101    FILES=`$NICE sqlite3 $LOCALDB "select replace(base_uri||'/'||filename,' ','%20') from photos limit $offset,$limit"`
102
103    if [ "x$FILES" != "x" ] ; then
104      #found some files, process them
105      for file in $FILES; do
106          # handle white space in file names
107          file=${file/file:\/\/$dbprefix/}
108          file=${file//\%20/ }
109          dir=`dirname "$file"`
110          base=`basename "$file"`
111
112          if [ ! -s "Photos-tiny/$dir/$base" ] ; then
113              mkdir -p Photos-tiny/$dir
114              $NICE convert "$dirprefix/$file" -auto-orient -resize x100 -quality 80% "Photos-tiny/$dir/$base"
115              if [ $removemeta=1 ]; then
116                  $NICE jhead -q -se -purejpg "Photos-tiny/$dir/$base"
117                  $NICE jhead -q -se -dt "Photos-tiny/$dir/$base"
118                  $NICE jhead -q -se -mkexif "Photos-tiny/$dir/$base"
119                  $NICE jhead -q -se -cl "This photo belongs to $admin and was taken from $webbase. If you want to use this photo, please contact him." "Photos-tiny/$dir/$base"
120              fi
121              done=$((done+1))
122          fi
123          if [ ! -s "Photos-small/$dir/$base" ] ; then
124              mkdir -p Photos-small/$dir
125              $NICE convert "$dirprefix/$file" -auto-orient -resize x600 -quality 80% "Photos-small/$dir/$base"
126              if [ $removemeta=1 ]; then
127                  $NICE jhead -q -se -purejpg "Photos-small/$dir/$base"
128                  $NICE jhead -q -se -dt "Photos-small/$dir/$base"
129                  $NICE jhead -q -se -mkexif "Photos-small/$dir/$base"
130                  $NICE jhead -q -se -cl "This photo belongs to $admin and was taken from $webbase. If you want to use this photo, please contact him." "Photos-small/$dir/$base"
131              fi
132              done=$((done+1))
133          fi
134
135          echo -n -e "$((done/2))       \r"
136      done
137    else
138        break;
139    fi
140
141    #get ready to get the next photos
142    offset=$((offset+$limit))
143 done