Skip to content

Instantly share code, notes, and snippets.

@Bombe
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save Bombe/d2895cc089f1ffc8b93f to your computer and use it in GitHub Desktop.

Select an option

Save Bombe/d2895cc089f1ffc8b93f to your computer and use it in GitHub Desktop.
Create pixelated versions of any number of album covers
#!/bin/bash
# GUID:370EBDBF-00CF-4D6D-9772-4A2B941A016F
PIXELS="12"
TARGET_WIDTH="900"
BORDER_WIDTH="50"
TEMP="temp"
OUTPUT_FILE="12x12.png"
IMAGES=()
while [ -n "${1}" ]; do
case "${1}" in
--pixels|-p)
PIXELS="${2}"
shift
;;
--width|-w)
TARGET_WIDTH="${2}"
shift
;;
--border|-b)
BORDER_WIDTH="${2}"
shift
;;
--output|-o)
OUTPUT_FILES="${2}"
shift
;;
-*)
echo "$0 [--pixels <pixels>] [--width <width>] [--border <border>] <image1> <image2> <image3> <image4>"
exit 1
;;
*)
IMAGES=("${IMAGES[@]}" "${1}")
esac
shift
done
trap removeTemp SIGHUP SIGINT SIGTERM
mkdir -p "${TEMP}"
function removeTemp {
rm -rf "${TEMP}"
exit
}
# create alpha channel
ppmmake 0,0,0 100 100 | pnmtopng | convert - -fill white -draw 'circle 50,50, 97,50' - | pngtopnm | pnmtile $(($PIXELS*100)) $(($PIXELS*100)) | pamscale --width=${TARGET_WIDTH} --height="${TARGET_WIDTH}" > "${TEMP}/alphachannel.pnm"
ppmmake 0,0,0, "${TARGET_WIDTH}" "${TARGET_WIDTH}" > "${TEMP}/black.pnm"
# scale picture down
function createSmallPicture {
anytopnm "${1}" 2> /dev/null | pamscale --width="${PIXELS}" --height="${PIXELS}" | pamscale --width="${TARGET_WIDTH}" --height="${TARGET_WIDTH}" > "${TEMP}/image.pnm"
cat "${TEMP}/black.pnm" | pamcomp --alpha="${TEMP}/alphachannel.pnm" "${TEMP}/image.pnm" | pnmpad --black --left="${BORDER_WIDTH}" --right="${BORDER_WIDTH}" --top="${BORDER_WIDTH}" --bottom="${BORDER_WIDTH}"
}
number="${#IMAGES[*]}"
across="1"
down="${number}"
getFactors() {
(
echo "number=${1}"
echo "s=sqrt(number)"
echo "while (s > 0) {"
echo "if (number % s == 0) {number/s; s; halt}"
echo "s=s-1"
echo "}"
) | bc
}
read across down <<<$(getFactors $number)
for ((d=0; d<${down}; d++)); do
for ((a=0; a<${across}; a++)); do
createSmallPicture "${IMAGES[$(($d*$across+$a))]}" > "${TEMP}/tile-$d-$a.pnm"
done
done
pamundice --across=${across} --down=${down} "${TEMP}/tile-%1d-%1a.pnm" | pnmtopng > "${OUTPUT_FILE}"
removeTemp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment