makemywall 3.1 KB

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