#!/bin/bash # # Usage: negate-white-on-black [--fix] file-or-directory # Without --fix will only print image filename that is assumed to be grayscale with "inverted" colormap # FIX=0 if [ "$1" = "--fix" ]; then FIX=1; shift; fi function negate(){ SRC="$1" (echo "${SRC}"; identify -verbose -unique "${SRC}") | awk ' BEGIN { isChannel = 0; isGray = 0; } NR == 1 { filename = $0; } /Channel statistics:/ { isChannel = 1; } /Colors:/ { isChannel = 0; exit 0;} /Gray:/ { if (isChannel == 1){ isGray = 1; } } /kurtosis:/ { if (isChannel == 1) kurtosis = $2; } /skewness:/ { if (isChannel == 1) skewness = $2; } /mean:/ { if (isChannel == 1){ gsub(/[()]/, "", $3); mean = $3 #printf "%s %s %s\n", $0, $3, mean; } } { next; } END { if (isGray == 1 && mean < 0.35){ #printf "%s %s %s %s\n", filename, mean, kurtosis, skewness; printf "%s\n", filename; } } ' | while read nfile; do echo "Found inverted file: $nfile" if [ $FIX = 1 ]; then echo "Converting: $nfile" cp "$nfile" "$nfile.negative" convert -negate "$nfile" "$nfile" else echo "Skipping: $nfile" fi done } if [ -d "$1" ]; then find $1 -name '*.png' -o -name '*.jpg' | while read file; do negate $file done else negate $1 fi exit 0