Making a PDF grayscale with ghostscript
September 30th 2008

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: , , , , , , ,

23 Comments »

23 Responses to “Making a PDF grayscale with ghostscript”

  1. Jose on 06 Nov 2008 at 4:19 am #

    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. CC on 02 Feb 2009 at 13:07 pm #

    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. isilanes on 02 Feb 2009 at 13:37 pm #

    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. CC on 02 Feb 2009 at 13:59 pm #

    Well, this is the link:
    http://knol.google.com/k/c-c/how-to-convert-a-vector-eps-color/1yx5vp4cwri6n/2#

    It is not flattering nor undeserved: I only think it could be useful (it was for me) to publish this with the correct references.
    And then..... I was eager to try this Google Knol ;-)

  5. Julien on 29 Oct 2010 at 22:59 pm #

    Thanks for the script, was really useful to me today!

  6. Marcello on 23 Mar 2011 at 10:28 am #

    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.

  7. Marcello on 23 Mar 2011 at 10:36 am #

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

  8. Paul on 30 Mar 2011 at 11:55 am #

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

    Thanks,
    Paul

  9. Michael on 12 Aug 2011 at 14:18 pm #

    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

  10. isilanes on 16 Aug 2011 at 9:09 am #

    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!

  11. Jonas on 09 Nov 2011 at 14:38 pm #

    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

  12. mandev on 19 Dec 2011 at 14:11 pm #

    @Jonas : -sCCompatibilityLevel=1.5 is wrong => -sCompatibilityLevel=1.5

  13. 2xJoe on 22 Dec 2011 at 11:53 am #

    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."

  14. Sherilyn Muckelroy on 05 Jan 2012 at 9:37 am #

    Thanks, bookmarked!

  15. hyangyt on 24 Jan 2012 at 14:35 pm #

    Hi,

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

  16. hyangyt on 24 Jan 2012 at 14:57 pm #

    This code works fine(for major cases) (see http://superuser.com/questions/200378/converting-a-pdf-to-black-white-with-ghostscript)

    pdf2ps color.pdf color.ps

    gs \
    -o bw-from-color.pdf \
    -sDEVICE=pdfwrite \
    -c "/setrgbcolor{0 mul 3 1 roll 0 mul 3 1 roll 0 mul 3 1 roll 0 mul add add setgray}def" \
    -f color.ps

    but how to convert color pdf to monochrome pdf directly(without converting it to ps first)?

  17. isilanes on 24 Jan 2012 at 16:16 pm #

    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.

  18. hyangyt on 25 Jan 2012 at 4:28 am #

    Hi,

    I found a way to do it, please see my post:

    http://yentaublog.wordpress.com/2012/01/25/convert-pdfs-to-blackwhite-translated-from-chinese/

  19. hyangyt on 25 Jan 2012 at 4:30 am #

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

  20. isilanes on 25 Jan 2012 at 9:34 am #

    Thanks hyangyt! Very nice of you to share your solution.

  21. jan on 21 May 2012 at 1:08 am #

    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.

  22. isilanes on 21 May 2012 at 8:39 am #

    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.

  23. Setting Color LaTeX Generated PDF To Print In Black & White | Click & Find Answer ! on 21 Apr 2013 at 16:14 pm #

    [...] black and white rather than greyscale.You can Ghostscript to convert a colour PDF to greyscale. See http://handyfloss.net/2008.09/making-a-pdf-grayscale-with-ghostscript/ .However, you’d get the same result just setting your printer settings to [...]

Trackback URI | Comments RSS

Leave a Reply

Subscribe without commenting

« | »