|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+#!/bin/sh
|
|
|
2
|
+
|
|
|
3
|
+# Copyright 2017 jstpcs <jstpcs at protonmail dot com>
|
|
|
4
|
+# Copyright 2017 moviuro <moviuro at gmail dot com>
|
|
|
5
|
+
|
|
|
6
|
+# Redistribution and use in source and binary forms, with or without
|
|
|
7
|
+# modification, are permitted provided that the following conditions are met:
|
|
|
8
|
+#
|
|
|
9
|
+# 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
10
|
+# list of conditions and the following disclaimer.
|
|
|
11
|
+#
|
|
|
12
|
+# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
13
|
+# this list of conditions and the following disclaimer in the documentation
|
|
|
14
|
+# and/or other materials provided with the distribution.
|
|
|
15
|
+#
|
|
|
16
|
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
17
|
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
18
|
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
19
|
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
20
|
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
21
|
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
22
|
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
23
|
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
24
|
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
25
|
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
26
|
+
|
|
|
27
|
+set -e
|
|
|
28
|
+
|
|
|
29
|
+usage() {
|
|
|
30
|
+ cat << EOH
|
|
|
31
|
+Generate wallpapers to a specified size, given a base picture
|
|
|
32
|
+
|
|
|
33
|
+$0 [-vs] width height [picture [picture [...]]]
|
|
|
34
|
+$0 -h
|
|
|
35
|
+
|
|
|
36
|
+-h : displays this help message
|
|
|
37
|
+-s : use a "safer" and slower method to get the background color
|
|
|
38
|
+-v : be verbose (set -x)
|
|
|
39
|
+
|
|
|
40
|
+Example: $0 1920 1080 coolimage.png
|
|
|
41
|
+
|
|
|
42
|
+The following syntax also works:
|
|
|
43
|
+find . -type f -name '*.png' | $0 1080 1920
|
|
|
44
|
+EOH
|
|
|
45
|
+ exit ${1:-0}
|
|
|
46
|
+}
|
|
|
47
|
+
|
|
|
48
|
+die() {
|
|
|
49
|
+ echo "$1" && exit "${2:-1}"
|
|
|
50
|
+}
|
|
|
51
|
+
|
|
|
52
|
+while getopts ":hvs" _opt; do
|
|
|
53
|
+ case "$_opt" in
|
|
|
54
|
+ h) usage ;;
|
|
|
55
|
+ s) _slow=1 ;;
|
|
|
56
|
+ v) set -x ;;
|
|
|
57
|
+ *) usage 1 >&2 ;;
|
|
|
58
|
+ esac
|
|
|
59
|
+done
|
|
|
60
|
+
|
|
|
61
|
+shift $((OPTIND-1))
|
|
|
62
|
+
|
|
|
63
|
+if [ -n "$_slow" ]; then
|
|
|
64
|
+ getbg() {
|
|
|
65
|
+ # We get all hexa codes of all pixels in the picture, and pick the most
|
|
|
66
|
+ # frequent one... which should be the background color?
|
|
|
67
|
+ convert "$1" -format %c +dither -depth 5 histogram:info: |
|
|
|
68
|
+ sort -nr | head -1 | grep -Eo '#[A-F0-9]{6}'
|
|
|
69
|
+ }
|
|
|
70
|
+else
|
|
|
71
|
+ getbg() {
|
|
|
72
|
+ # We get the color of the top-left pixel.
|
|
|
73
|
+ convert "$1" -crop 1x1+0+0 -depth 8 txt: | grep -Eo '#[A-F0-9]{6}'
|
|
|
74
|
+ }
|
|
|
75
|
+fi
|
|
|
76
|
+
|
|
|
77
|
+# We require width & height
|
|
|
78
|
+# TODO: maybe auto-detect? seems tricky with dual screen
|
|
|
79
|
+if [ $# -lt 2 ]; then
|
|
|
80
|
+ usage 1 >&2
|
|
|
81
|
+fi
|
|
|
82
|
+width="$1"
|
|
|
83
|
+height="$2"
|
|
|
84
|
+shift 2
|
|
|
85
|
+
|
|
|
86
|
+{ if [ -z "$1" ]; then
|
|
|
87
|
+ # No arguments, we read stdin
|
|
|
88
|
+ cat -
|
|
|
89
|
+else
|
|
|
90
|
+ for _file in "$@"; do
|
|
|
91
|
+ printf '%s\n' "$_file"
|
|
|
92
|
+ done
|
|
|
93
|
+fi } |
|
|
|
94
|
+while IFS= read -r inpfile; do
|
|
|
95
|
+ outfile="$(basename "$inpfile")"
|
|
|
96
|
+ bgcolor="$(getbg "$inpfile")"
|
|
|
97
|
+
|
|
|
98
|
+ # We use the `-resize ..x..\>` syntax to only shrink pictures. Enlarging is
|
|
|
99
|
+ # asking for trouble (artifacting, etc.)
|
|
|
100
|
+ convert "$inpfile" -gravity center -resize "${width}x${height}>" \
|
|
|
101
|
+ -background "$bgcolor" -extent "${width}x${height}" \
|
|
|
102
|
+ "${outfile%.*}-${width}x${height}.${outfile##*.}"
|
|
|
103
|
+done
|