main dots / scripts / Scripts / shell / dither.sh
xxwhirlpool  ·  2026-07-28
 1#!/usr/bin/env bash
 2#
 3# dither images in monochrome
 4#
 5# inspired by this blog:
 6# https://dead.garden/blog/how-my-images-are-dithered.html
 7
 8if [[ "$#" -eq 0 ]]; then
 9	echo "usage: dither.sh [FILENAME]"
10	exit 1
11fi
12
13# vars
14img="$1"
15imgorig="${img%.*}_orig.${img##*.}"
16imgnew="${img%.*}_dither.${img##*.}"
17
18# cp img so there's a backup
19cp "$img" "$imgorig"
20
21# pick color palette as
22# described in filenames
23palette=$(gum file "./palettes_dither")
24
25# do the conversion
26magick "$imgorig" -resize 800 -set option:distort:viewport '%wx%h+0+0' -colorspace CMYK -separate null: \( -size 2x2 xc: \( +clone -negate \) +append \( +clone -negate \) -append \)  -virtual-pixel tile -filter gaussian  \( +clone -distort SRT 1,0 \) +swap  \( +clone -distort SRT 1,15 \) +swap   \( +clone -distort SRT 1,45 \) +swap  \( +clone -distort SRT 1,75 \) +swap +delete -compose Overlay -layers composite -set colorspace CMYK -combine -remap "$palette" -colors 32 "$imgnew"