1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. Usage: $0 [-s] <source file> <width> <height> : creates a wallpaper
  28. -s : use a "safer" and slower method to get the background color
  29. $0 -h : displays this help message
  30. Example: $0 coolimage.png 1920 1080
  31. EOH
  32. exit ${1:-0}
  33. }
  34. die() {
  35. echo "$1" && exit "${2:-1}"
  36. }
  37. if [ "$1" = "-h" ]; then
  38. usage
  39. fi
  40. if [ "$1" = "-s" ]; then
  41. getbg() {
  42. # We get all hexa codes of all pixels in the picture, and pick the most
  43. # frequent one... which should be the background color?
  44. convert "$1" -format %c +dither -depth 5 histogram:info: |
  45. sort -nr | head -1 | grep -Eo '#[A-F0-9]{6}'
  46. }
  47. shift
  48. else
  49. getbg() {
  50. # We get the color of the top-left pixel.
  51. convert "$1" -crop 1x1+0+0 -depth 8 txt: | grep -Eo '#[A-F0-9]{6}'
  52. }
  53. fi
  54. if [ $# -ne 3 ]; then
  55. usage 1 >&2
  56. fi
  57. [ -r "$1" ] || die "File $1 is not readable!"
  58. inpfile="$1"
  59. outfile="$(basename "$inpfile")"
  60. width="$2"
  61. height="$3"
  62. bgcolor="$(getbg "$inpfile")"
  63. # We use the `-resize ..x..\>` syntax to only shrink pictures. Enlarging is
  64. # asking for trouble (artifacting, etc.)
  65. convert "$inpfile" -gravity center -resize "${width}x${height}>" \
  66. -background "$bgcolor" -extent "${width}x${height}" \
  67. "${outfile%.*}-${width}x${height}.${outfile##*.}"