d1be98dc7df13d6d7e00d80846abce00e35b4197
[photo-tags.git] / create_thumbnails.sh
1 #!/bin/bash
2
3 NICE="nice -19"
4
5 # init some variables
6 removemeta=0
7
8 # parse ini file
9 CONFIG_FILE="config.ini"
10
11 # copied and modified from http://mark.aufflick.com/blog/2007/11/08/parsing-ini-files-with-sed
12 eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
13     -e 's/;.*$//' \
14     -e 's/\[.*\]//' \
15     -e 's/[[:space:]]*$//' \
16     -e 's/^[[:space:]]*//' \
17     -e "s/^\(.*\)=\([^\"']*\)$/\1=\"\2\"/" \
18    < $CONFIG_FILE `
19
20 LOCALDB=$fspotdb
21 dbprefix=${dbprefix//\//\\\/}
22 limit=`$NICE sqlite3 $LOCALDB "select count(*) from photos"`
23
24
25 # parse command line
26 while getopts ":rl:" opt; do
27   case $opt in
28     r)
29       removemeta=1
30       ;;
31     l)
32       limit=$OPTARG
33       ;;
34     \?)
35       echo "Invalid option: -$OPTARG" >&2
36       exit 1
37       ;;
38     :)
39       echo "Option -$OPTARG requires an argument." >&2
40       exit 1
41       ;;
42   esac
43 done
44
45
46 #make sure we are in the correct directory
47
48 #make sure we can access the database
49 if [ ! -s $LOCALDB ] ; then
50     echo "Can't find database...exiting."
51     exit 1
52 fi
53
54 #create directories, iff they don't exist
55 if [ ! -d "Photos-small" ] ; then
56     echo "creating local directory to store photos of reduced size"
57     mkdir Photos-small
58 fi
59
60 if [ ! -d "Photos-tiny" ] ; then
61     echo "creating local directory to store thumbnails"
62     mkdir Photos-tiny
63 fi
64
65 done=0
66 offset=0
67 FILES=""
68
69 while [ $done -lt $limit ] ; do
70    echo "skipping $offset pics, getting $limit new pics to work on..."
71    # handle white space in filename correctly
72    FILES=`$NICE sqlite3 $LOCALDB "select replace(base_uri||'/'||filename,' ','%20') from photos limit $offset,$limit"`
73
74    if [ "x$FILES" != "x" ] ; then
75      #found some files, process them
76      for file in $FILES; do
77          # handle white space in file names
78          file=${file/file:\/\/$dbprefix/}
79          file=${file//\%20/ }
80          dir=`dirname "$file"`
81          base=`basename "$file"`
82
83          if [ ! -s "Photos-tiny/$dir/$base" ] ; then
84              mkdir -p Photos-tiny/$dir
85              $NICE convert "$dirprefix/$file" -auto-orient -resize x100 -quality 80% "Photos-tiny/$dir/$base"
86              if [ $removemeta=1 ]; then
87                  $NICE jhead -q -se -purejpg "Photos-tiny/$dir/$base"
88                  $NICE jhead -q -se -dt "Photos-tiny/$dir/$base"
89                  $NICE jhead -q -se -mkexif "Photos-tiny/$dir/$base"
90                  $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"
91              fi
92              done=$((done+1))
93          fi
94          if [ ! -s "Photos-small/$dir/$base" ] ; then
95              mkdir -p Photos-small/$dir
96              $NICE convert "$dirprefix/$file" -auto-orient -resize x600 -quality 80% "Photos-small/$dir/$base"
97              if [ $removemeta=1 ]; then
98                  $NICE jhead -q -se -purejpg "Photos-small/$dir/$base"
99                  $NICE jhead -q -se -dt "Photos-small/$dir/$base"
100                  $NICE jhead -q -se -mkexif "Photos-small/$dir/$base"
101                  $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"
102              fi
103              done=$((done+1))
104          fi
105
106          echo -n -e "$((done/2))       \r"
107      done
108    else
109        break;
110    fi
111
112    #get ready to get the next photos
113    offset=$((offset+$limit))
114 done