-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshotwell-thumbs
executable file
·140 lines (119 loc) · 4.02 KB
/
shotwell-thumbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
#=======================================================================================================================
# Description
# Script to (re)generate thumbnails for Shotwell
# Synopsis
# shotwell-thumbs [options]
# Author
# Dmitry Kann, https://yktoo.com/
# License
# Public domain
#=======================================================================================================================
. "$(dirname "$(realpath "$0")")/common.sh"
# Setup vars
DB_FILE="$HOME/.local/share/shotwell/data/photo.db"
THUMB_BASE_DIR="$HOME/.cache/shotwell/thumbs/"
USAGE_INFO="Script for (re)generating Shotwell thumbnails. Written by Dmitry Kann, https://yktoo.com/
Usage: $0 [options]
Options:
-h Print out this help message and exit
-r Regenerate and overwrite thumbnail even if it exists
-128 Generate 128x128 thumbnail
-360 Generate 360x360 thumbnail
If neither -128 nor -360 is given, both sizes are generated."
# Get TTY width
tty_width=$(stty size | cut -d ' ' -f 2)
# Prints out a status message (overwritten every time)
# Arguments:
# $1 message text
status() {
# Pad/truncate the line to fit screen width
printf "%-${tty_width}.${tty_width}s\r" "$1"
}
# Prints out a progress message
# Arguments:
# $1 current item number
# $2 total number of items
# $3 message text
progress() {
printf -v msg '[%d/%d] %s' "$1" "$2" "$3"
status "$msg"
}
# Generates a thumbnail file
# Arguments:
# $1 thumbnail size, either 128 or 360
# $2 photo ID
# $3 photo file name
# $4 number of current photo
# $5 total number of photos
# Returns:
# Nonzero value if thumbnail generation was skipped
generate_thumb() {
size="$1"
id="$2"
pic_file="$3"
thumb_file=$(printf "$THUMB_BASE_DIR/thumbs${size}/thumb%016x.jpg" "$id")
# If force regenerate or thumb file doesn't exist yet
if ((regenerate != 0)) || [[ ! -f "$thumb_file" ]]; then
progress "$4" "$5" "Generating $size thumbnail for $pic_file"
convert "$pic_file" -auto-orient -thumbnail "${size}x${size}" "$thumb_file"
if (($? != 0)); then
status ""
err "Generating thumbnail for $pic_file failed"
fi
return 0
else
progress "$4" "$5" "Skipping $size thumbnail for $pic_file"
return 1
fi
}
#-----------------------------------------------------------------------------------------------------------------------
# Main routine
#-----------------------------------------------------------------------------------------------------------------------
# Trap Ctrl-C
trap interrupted INT
# Validate paths
[[ -f "$DB_FILE" ]] || err "Shotwell database file $DB_FILE doesn't exist"
[[ -r "$DB_FILE" ]] || err "Shotwell database file $DB_FILE isn't readable"
[[ -d "$THUMB_BASE_DIR" ]] || err "Shotwell thumbnail base directory $THUMB_BASE_DIR doesn't exist"
# Parse the command line
regenerate=0
thumb128=
thumb360=
for arg; do
case "$arg" in
-h)
usage
;;
-r)
regenerate=1
;;
-128)
thumb128=1
;;
-360)
thumb360=1
;;
*)
usage "Unknown option: '$arg'"
esac
done
# If no size explicitly specified, activate both of them
if [[ -z "$thumb128" ]] && [[ -z "$thumb360" ]]; then
thumb128=1
thumb360=1
fi
# Get the total number of photos
count_total=$(sqlite3 "$DB_FILE" "select count(*) from PhotoTable")
((thumb128 != 0 && thumb360 != 0)) && ((count_total *= 2))
# Iterate over the files in reverse chronological order
count_cur=0
count_done=0
sqlite3 "$DB_FILE" "select id||' '||filename from PhotoTable order by timestamp desc" |
while read pic_id pic_file; do
((thumb128 != 0)) && generate_thumb 128 "$pic_id" "$pic_file" "$((++count_cur))" "$count_total" && ((count_done++))
((thumb360 != 0)) && generate_thumb 360 "$pic_id" "$pic_file" "$((++count_cur))" "$count_total" && ((count_done++))
done
echo
status "Done. $count_total thumbnails processed, $count_done thumbnails generated"
echo