Categories
Linux

Counting color and black/white pages in a pdf

A little tip to count how many color pages there are in a pdf file using Ghostscript.

If you want to estimate the printing cost of a pdf, it is often helpful to know the exact number of color pages inside it.

Using the command below you can get a CMYK color report for each page of the pdf file:

gs -o - -sDEVICE=inkcov main.pdf

, where main.pdf is the name of the pdf file you are analyzing. The output of the command will be like this:

GPL Ghostscript 9.14 (2014-03-26)
Copyright (C) 2014 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 220.
Page 1
0.00245  0.00239  0.00091  0.02255 CMYK OK
Page 2
0.00000  0.00000  0.00000  0.01491 CMYK OK
Page 3
0.00000  0.00000  0.00000  0.04370 CMYK OK
...

For each page the four numbers, represent the CMYK color coverage of the page (Cyan, Magenta, Yellow, and blacK). If the first three numbers are different from zero, the page uses some of the colors. If the last number is the only non-zero element then the page is rendered with Black color only. 

So it’s easy to use the above command to count the number of color pages, with the following command:

gs -o - -sDEVICE=inkcov main.pdf | grep -v "^ 0.00000  0.00000  0.00000" | grep "^ " | wc -l

The output will be a single number representing the number of output lines that do not have zero values for each of the CMY color components, aka the color pages number.

Leave a Reply

Your email address will not be published. Required fields are marked *