Skip to content

Instantly share code, notes, and snippets.

@clemsos
Created July 29, 2014 10:09
Show Gist options
  • Save clemsos/0cdfd19e0b13776951ca to your computer and use it in GitHub Desktop.
Save clemsos/0cdfd19e0b13776951ca to your computer and use it in GitHub Desktop.
Count color and B&W pages in a PDF
#!/bin/bash
file="$1"
colorpages=0
# count all pages
totalpages=$(gs -q -dNODISPLAY -c "($1) (r) file runpdfbegin pdfpagecount = quit")
echo "Total pages : $totalpages"
# find pages with colors
for page in $(identify -density 12 -format '%p ' "$file") ; do
if convert "$file[$((page-1))]" -colorspace RGB -unique-colors txt:- | sed -e 1d | egrep -q -v ': \(\s*([0-9]*),\s*\1,\s*\1' ; then
colorpages=$((colorpages+1))
echo $page
fi
done
#show results
echo "Colors : $colorpages"
echo "B&W : $(($totalpages-$colorpages))"
echo "Total pages : $totalpages"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment