Making a PDF grayscale with ghostscript

A request from a friend made me face the problem of converting a color PDF into a grayscale one. Searching the web provided some ways of doing so with Adobe Acrobat, via some obscure menu item somewhere.

However, the very same operation could be undertaken with free tools, such as ghostscript. I found a way to do it in the YANUB blog, and I will copy-paste it here, with a small modification.

Assuming we have a file called color.pdf, and we want to convert it into grayscale.pdf, we could run the following command (all in a single line, and omitting the "\" line continuation marks):

% gs -sOutputFile=grayscale.pdf -sDEVICE=pdfwrite \
-sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray \
-dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH color.pdf

I prefer the above to YANUB's version below (in red what he lacks, in blue what I lack), because a shell operation is substituted by some option(s) of the command we are running:

% gs -sOutputFile=grayscale.pdf -sDEVICE=pdfwrite \
-sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray \
-dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH color.pdf < /dev/null

A sample Perl script to alleviate the tedious writing above:

#!/usr/bin/perl -w
use strict;
my $infile = $ARGV[0];
my $outfile = $infile;
$outfile =~ s/\.pdf$//;
$outfile = $outfile."_gray.pdf";
system "gs -sOutputFile=$outfile -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH $infile"

Assuming we call the Perl script "togray.pl", and that we have a color file "input.pdf", we could just issue the command:

% togray.pl input.pdf

and we would get a grayscale version of it, named "input_gray.pdf".

Tags: , , , , , , ,

26 thoughts on “Making a PDF grayscale with ghostscript

  1. Hi,

    Great post and it also works in Windows with Ghostview (http://pages.cs.wisc.edu/~ghost/doc/GPL/gpl863.htm).

    The only thing that I had to change was the exe.

    gswin32.exe -sOutputFile=grayscale.pdf -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH color.pdf

    I did try the perl script but it should work also.

    Thanks,
    Jose.

  2. Hello,

    this is very useful; I just have to add a couple of commands to go back to the EPS format without loss of quality.

    >> pdfcrop grayscale.pdf

    This command let you remove the white spaces around the picture (introduced with the previous command): this is particularly useful if you want to insert the picture into a LaTeX file. The standard output file name is grayscale-crop.pdf (the -crop part is added)

    >> pdftops -eps grayscale-crop.pdf final.eps

    Finally, you revert your pdf file to the original eps format.

    I searched for such a solution for a long time: I am going to add the whole tutorial to Goggle Knol, obviously citing this webpage: I hope it is convenient for you!

  3. Thanks CC,

    The contents of my blog are licensed under a Creative Commons license (ups, I thought I had a tag somewhere, but now I only see the "Copyright (c) handyfloss 2009" at the bottom. I'll add a CC tag some day), and you may use them any way you see fit, provided that you cite the source, and you make no commercial use of it.

    So, yes, putting it in Knol is fine with me. Actually I find it very flattering (and undeserved).

  4. Hi, thanks for the script, but it doesn't work for me. I have a PDF obtained from scanning a paper doc. Each page is composed of a single, full-page embedded jpeg (color) image.
    I still haven't found a way to convert that file to grayscale without resorting to manual, single-image conversion.

    Thanks anyway. Page bookmarked.

  5. Sorry, my bad, I actually forgot the "=Gray" bit after -sColorConversionStragegy! Now it works like a charm, thank you!!

  6. Hi! Great little script. Makes my live a lot easier when writing my thesis.

    Thanks,
    Paul

  7. Hi! Excellent work, thanks!

    However, I would need additional functionality as my target is to convert a PDF into black & white (monochrome). I would like to define a threshold - actually I am most interested in a threshold value of 100%: Black pixels (rather vectors) should remain black and everything that is not purely black should disappear (i.e. turn into white).

    The following link refers to similar ideas: http://www.rhinocerus.net/forum/lang-postscript/354259-using-ghostscript-convert-postscript-pure-black-white.html
    However, I cannot apply the parameters correctly.

    Could any of you gurus help me and provide a proper ghostscript command line that does the desired job?

    Many thanks in advance
    Michael

  8. Sorry Michael, I don't know how to help you. The thread you link to contains a couple of interesting solutions. I understand that they didn't work for you, in which case I really don't have further ideas (even the ones in that thread were above me). Good luck!

  9. Hi
    just what I want to do, but I get the error below. And I can't figure out whats wrong.

    gs -sOutputFile=grayscale.pdf -sDEVICE=pdfwrite -sCCompatibilityLevel=1.5 -dProcessColorModel=/DeviceGray -sColorConversionStrategy=Gray -dNOPAUSE -dBATCH 03_00_00_551.pdf

    ESP Ghostscript 815.02 (2006-04-19)
    Copyright (C) 2004 artofcode LLC, Benicia, CA. All rights reserved.
    This software comes with NO WARRANTY: see the file PUBLIC for details.
    CRIT: rangecheck in .putdeviceprop

  10. This hint was very usefull to me, thanx a lot!
    GS does the magic that corel and cs5 denied to me due to a obscure "host application error."

  11. Hi,

    is there any way to convert a pdf to pure black & white (monochrome) with ghostscript? Thanks.

  12. Hi hyangyt,

    I can't really help you. Is there any reason why PDF -> PS -> PDF is not acceptable for you?

    I have tried the code you show, and it actually works on PS files, but not PDFs (if we do not convert the input PDF to PS before applying gs, the output PDF is still a color one). It strikes me as funny, since gs should also handle PDFs!

    I must also say that your code seems to convert text to B/W, but not images, at least in a couple of tries I've made.

  13. It converts PDF to DJVU first, then convert to B&W PDF. It seems work much better and faster, and works for images, too.

  14. thanks for the summary of the gs commands!

    tiny comment on the perl script -- if the file name doesn't end with .pdf, the input file will be overwritten. it might be better to check and add _gray at the end anyway, even if there's no .pdf ending.

  15. Hi jan,

    Thanks, your warning is so true! However, it a PDF without a .pdf ending is used, the user deserves whatever happens to it ;^)

    I'll check it and fix it.

  16. Pingback: Setting Color LaTeX Generated PDF To Print In Black & White | Click & Find Answer !

  17. Pingback: convert pdf to grayscale | Bitkorn Blog

  18. Hey,

    first of all: Thanks!
    Is it possible to modify the pearl script that I can use it for a bunch of pdf files and don't have to type in every single pdf file in a folder (maybe including subfolders)?

    Thanks again

    PS: I tried the command and the file size is not really smaller (max. 10%) I was assuming a higher reduction of the file size.

  19. The following Python script will convert all PDF files in a directory into grayscale, if called from within that directory:

    import glob
    import subprocess as sp
    
    for fn_in in glob.glob("*.pdf"):
        fn_out = fn_in.replace(".pdf",'') + "_gray.pdf"
    
        string = '{0} -> {1}'.format(fn_in, fn_out)
        print(string)
        
        cmnd = "gs -sOutputFile={1} -sDEVICE=pdfwrite -sColorConversionStrategy=Gray\
     -dProcessColorModel=/DeviceGray -dCompatibilityLevel=1.5 -dNOPAUSE -dBATCH {0}".format(fn_in, fn_out)
        s = sp.Popen(cmnd, shell=True)
        s.communicate()
    

Leave a Reply

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

Subscribe without commenting