summaryrefslogtreecommitdiff
path: root/wall_gen.sh
blob: 75b9114c544ebffd77550729713aa89441f95614 (plain)
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
#!/bin/sh
# requires imagemagick and colors (https://git.2f30.org/colors/)

# resolution of wallpaper
XMAX=1920
YMAX=1080

if ! [ -f "$1" ]; then
  printf "ERROR: file not found\n"
  exit 1
fi

# get resolution of cover
RES=$(identify "$1" | awk '{print $3}')
X=$(echo $RES | awk -F "x" '{print $1}')
Y=$(echo $RES | awk -F "x" '{print $2}')

X_RES=$(echo "$XMAX"/4 | bc) # x to resize to if x > y
Y_RES=$(echo "$YMAX"/2 | bc) # y to resize to if y > x should be ~400
FACT= # factor to change scale by; in %; defined next

# FACT will depend on X if cover is squarish, or on Y if it is more rectangular
if [ $Y -gt $(echo "scale = 0; $X * 1.1 / 1" | bc) ]; then
  FACT="$(echo "scale = 2; $Y_RES/$Y * 100 / 1" | bc)%" # divide by 1 forces bc to round
else
  FACT="$(echo "scale = 2; $X_RES/$X * 100 / 1" | bc)%" # divide by 1 forces bc to round
fi

mkdir -p /tmp/wall_gen
# get name and put .png to automatically convert to png if necessary
NAME=$(echo "$(basename "$1")" | sed -e 's/.jpg/.png/' -e 's/.jpeg/.png/' -e 's/.gif/.png/')
magick "$1" -resize "$FACT" /tmp/wall_gen/"$NAME"
FILE="/tmp/wall_gen/$NAME"
RES=$(identify "$FILE" | awk '{print $3}')
X=$(echo "$RES" | awk -F "x" '{print $1}')
Y=$(echo "$RES" | awk -F "x" '{print $2}')

# get how size of border; divide by 2 as border size is only one side's
XPLUS=$(echo "(1920-$X)/2" | bc)
YPLUS=$(echo "(1080-$Y)/2" | bc)

COLOR=$(cat "$FILE" | colors -n 1)

NAME="$(echo $NAME | sed 's/.png//')-wall.png"

magick "$FILE" -bordercolor "$COLOR" -border "$XPLUS"x"$YPLUS" /tmp/wall_gen/$NAME
xwallpaper --zoom /tmp/wall_gen/$NAME