px6/px6_convert.sh
2025-01-01 18:17:36 +01:00

29 lines
1.1 KiB
Bash
Executable file

#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash imagemagick
INPUT="$1"
OUTPUT="$2"
export OW="$3"
export OH="$4"
W="$5"
if [ "$OW" == "" ]; then OW=0; fi
if [ "$OH" == "" ]; then OH=0; fi
OPTIONS="-resize $W -set depth 8 -set colorspace RGB"
#initial conversion
convert $OPTIONS "$INPUT" txt:- | awk -v ow="$OW" -v oh="$OH" 'match($0, /^([0-9]*),([0-9]*):.*#([0-9A-Fa-f]{6,8}) +r/, arr ){ print "PX " arr[1]+ow " " arr[2]+oh " " arr[3] }' > "${OUTPUT}.tmp"
echo optimizing >&2
# edge detection
convert $OPTIONS "$INPUT" -canny 0x1+10%+30% txt:- | awk -v ow="$OW" -v oh="$OH" 'match($0, /^([0-9]*),([0-9]*):.*#000000/, arr ){ print "PX " arr[1]+ow " " arr[2]+oh " " }' > /tmp/px6_optimize
grep -Ff /tmp/px6_optimize "${OUTPUT}.tmp" >> "${OUTPUT}.tmp2"
cat "${OUTPUT}.tmp" > "${OUTPUT}.tmp3"
cat "${OUTPUT}.tmp2" >> "${OUTPUT}.tmp3"
cat "${OUTPUT}.tmp2" >> "${OUTPUT}.tmp3"
cat "${OUTPUT}.tmp2" >> "${OUTPUT}.tmp3"
cat "${OUTPUT}.tmp2" >> "${OUTPUT}.tmp3"
cat "${OUTPUT}.tmp3" | grep -ve " [a-fA-F0-9]\{6\}00" | shuf > /tmp/px6_optimize
mv /tmp/px6_optimize "$OUTPUT"
rm "${OUTPUT}.tmp" "${OUTPUT}.tmp2" "${OUTPUT}.tmp3"