summaryrefslogtreecommitdiff
path: root/.local/bin/convwall
blob: 21645240f2363f0f9299f10663d25adc04737a00 (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
#!/bin/sh

# 16:9 screen aspect ratio
# 16*100/9 == 177
wratio=16
hratio=9
chopnum=99%

width=$(identify -format '%w' "$1")
height=$(identify -format '%h' "$1")

ratio=$(echo "$wratio*100/$hratio" | bc)
dim=$(echo "$width*100/$height" | bc)

if [ "$dim" -ne "$ratio" ]; then
	if [ "$dim" -lt "$ratio" ]; then
		chopgeom="${chopnum}x0"	
		width=$(echo "$height*$wratio/$hratio" | bc)
	else
		chopgeom="0x$chopnum"	
		height=$(echo "$width*$hratio/$wratio" | bc)
	fi
	# may throw error "sort: write failed: 'standard output': Broken pipe \n sort: write error"
	# caused by head exit (or close stdin) before sort output complete, can ignore
	# https://stackoverflow.com/questions/46202653/bash-error-in-sort-sort-write-failed-standard-output-broken-pipe
	bgcolor=$(convert "$1" -gravity center -chop "$chopgeom" -define histogram:unique-colors=true -format %c histogram:info:- | sort -rn | head -n1 | awk \{print\ \$3\} | cut -c1-7)
	# another approach is to use "$width" or "x$height" for geometry, current approach is more readable
	convert "$1" -gravity center -background "$bgcolor" -extent "${width}x$height" "$2"
else
	echo 'same resolution ratio, no need to convert' >&2
	exit 1
fi