Explorar el Código

makemywall: new interface

We support new ways of reading files that we want to modify/generate:

% ./makemywall 1920 1080 myfile1.png myfile2.png
% ./makemywall 1920 1080 < mylist.txt
% find . -type f -name '*.png' | ./makemywall 1080 1920
Moviuro hace 8 años
padre
commit
7a3cd41678
Se han modificado 1 ficheros con 47 adiciones y 21 borrados
  1. 47
    21
      makemywall

+ 47
- 21
makemywall Ver fichero

@@ -28,10 +28,19 @@ set -e
28 28
 
29 29
 usage() { 
30 30
   cat << EOH
31
-Usage:   $0 [-s] <source file> <width> <height> : creates a wallpaper
32
-            -s : use a "safer" and slower method to get the background color
33
-         $0 -h : displays this help message
34
-Example: $0 coolimage.png 1920 1080
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
35 44
 EOH
36 45
   exit ${1:-0}
37 46
 }
@@ -40,18 +49,24 @@ die() {
40 49
   echo "$1" && exit "${2:-1}"
41 50
 }
42 51
 
43
-if [ "$1" = "-h" ]; then
44
-  usage
45
-fi
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))
46 62
 
47
-if [ "$1" = "-s" ]; then
63
+if [ -n "$_slow" ]; then
48 64
   getbg() {
49 65
     # We get all hexa codes of all pixels in the picture, and pick the most
50 66
     # frequent one... which should be the background color?
51 67
     convert "$1" -format %c +dither -depth 5  histogram:info: |
52 68
       sort -nr | head -1 | grep -Eo '#[A-F0-9]{6}'
53 69
   }
54
-  shift
55 70
 else
56 71
   getbg() {
57 72
     # We get the color of the top-left pixel.
@@ -59,19 +74,30 @@ else
59 74
   }
60 75
 fi
61 76
 
62
-if [  $# -ne 3 ]; then 
77
+# We require width & height
78
+# TODO: maybe auto-detect? seems tricky with dual screen
79
+if [ $# -lt 2 ]; then
63 80
   usage 1 >&2
64 81
 fi
82
+width="$1"
83
+height="$2"
84
+shift 2
65 85
 
66
-[ -r "$1" ] || die "File $1 is not readable!"
67
-inpfile="$1"
68
-outfile="$(basename "$inpfile")"
69
-width="$2"
70
-height="$3"
71
-bgcolor="$(getbg "$inpfile")"
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")"
72 97
 
73
-# We use the `-resize ..x..\>` syntax to only shrink pictures. Enlarging is
74
-# asking for trouble (artifacting, etc.)
75
-convert "$inpfile" -gravity center -resize "${width}x${height}>" \
76
-  -background "$bgcolor" -extent "${width}x${height}" \
77
-  "${outfile%.*}-${width}x${height}.${outfile##*.}"
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