Getting Shades of Gray is hard

How Grayify Pages in Your PDF

Sometimes you want to convert your PDF to gray-scale, other times you want everything gray except a few chosen pages. I discuss a small software package for the latter task.

TL;DR: check output my project, but be warned about the result of any gray conversion

I was printing my thesis recently. Some figures contain only a few dots of color, so I wanted to avoid paying for color pages when there is only color for cosmetic reasons on it. So I took this as a pretense to code a wrapper for pdftk that allowed me to keep a specified set of pages in color, while everything else is converted to gray-scale. At the end of the day, my effort was in vain: a lot of copy shops offer the option to only print a selection of pages in color. Just that cheap one I had been to previously was not capable of doing so…

Okay, so in principle, converting your PDF to gray-scale is as simple as:

    gs \
    -sDEVICE=pdfwrite \
    -sProcessColorModel=DeviceGray \
    -sColorConversionStrategy=Gray \
    -dPDFUseOldCMS=false \
    -dOverrideICC \
    -o output.pdf \
    -f input.pdf

After some research online, I found out that the result may appear gray, however that there is no way to guarantee that the resulting gray will not be mixed by taking equal parts of the three base colors. Recent versions of ghostscript come with an ink coverage device and allow you to determine how much of each page is covered in cyan (C), magenta (M), yellow (Y) and black (K). Using this command, gs prints these values in the aforementioned order:

$ gs  -o - -sDEVICE=inkcov input.pdf
 0.02843  0.02843  0.02843  0.05960 CMYK OK
 0.04589  0.04605  0.04598  0.08132 CMYK OK

So if the first three values equal 0, then you have got your all-back-and-white page. Otherwise, your printer will utilize color. If the initial three numbers are equal, the page will appear gray, but still consumes color ink. That is bad.

Well, if for any reason, you still want to continue to partially convert your PDF to gray, check out grayify-pdf. Given

  • your input PDF input.pdf
  • and file cpagesfile containing the target set of color pages (one page per line),

the command

greyifypdf input.pdf cpagesfile

produces a PDF where the specified pages are in color and everything else is in gray.

…and sorry for the cmake/c++11 dependency, but bash-only would have been less fun.

Post a Comment

All comments are held for moderation; Markdown formatting accepted;
By posting you agree to the following privacy policy.

This is a honeypot form. Do not use this form unless you want to get your IP address blacklisted. Use the second form below for comments.
Name: (required)
E-mail: (required, not published)
Website: (optional)
Name: (required)
E-mail: (required, not published)
Website: (optional)