Convert a PDF to greyscale on the command line in FLOSS?

60,331

Solution 1

ImageMagick can do this.

convert -colorspace GRAY color.pdf gray.pdf

via this email

Solution 2

Better:

gs \
 -sOutputFile=output.pdf \
 -sDEVICE=pdfwrite \
 -sColorConversionStrategy=Gray \
 -dProcessColorModel=/DeviceGray \
 -dCompatibilityLevel=1.4 \
 -dNOPAUSE \
 -dBATCH \
 input.pdf

Solution 3

Here’s a little script which in addition to the grayscale conversion can concatenate multiple input files. To use the script, put the following lines in a file, e.g. "convert2gray.sh"

#!/bin/bash
gs -sOutputFile=converted.pdf -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompatibiltyLevel=1.4 -dNOPAUSE -dBATCH $@

and make it executable

chmod +x convert2gray.sh

Then

./convert2gray.sh input1.pdf input2.pdf … lastinput.pdf

will produce a single PDF "converted.pdf", which contains all pages from the input files converted to grayscale.

I had to print out mutliple files all in grayscale and found this the easiest way, since you can print out everything after inpection with one command.

Share:
60,331

Related videos on Youtube

Amandasaurus
Author by

Amandasaurus

I'm a Linux user

Updated on September 17, 2022

Comments

  • Amandasaurus
    Amandasaurus over 1 year

    I have a colour PDF file, and I'm going to print it out and then photocopy it in black and white. I'd like to know what it's like in B&W before photocopying it. Is it possible to 'greyscale' a PDF on the command line using free software? I'm using Ubuntu 9.10.

  • tdc
    tdc almost 12 years
    Agreed, this gives much better results than convert, but sometimes it rotates the pdf which is a bit annoying!
  • tdc
    tdc almost 12 years
    Just realised you can disable that with -dAutoRotatePages=/None
  • Archie
    Archie over 11 years
    I just ran this command on a 58MB PDF that was already greyscale (came from a scanner) and the resulting output was 10MB and looked exactly the same. Nice!
  • Johannes Weiss
    Johannes Weiss about 11 years
    That significantly reduces quality. @goyinux' solution is better.
  • ixe013
    ixe013 over 9 years
    Works well on Windows, too! Just remove the `\` and put everything on the same line.
  • m000
    m000 over 9 years
    Convert will actually rasterize the contents of the pdf. So unless the pdf already encapsulates only raster images (e.g. a scanned document), this approach is a big no-no.
  • burtek
    burtek over 8 years
    Unless you use -density 400 -quality 100 parameters - that works well
  • jjmerelo
    jjmerelo over 8 years
    In fact, this fails with this error GPL Ghostscript 9.10: Unable to convert color space to Gray, reverting strategy to LeaveColorUnchanged.
  • saeedgnu
    saeedgnu almost 7 years
    Works great. But output image size is almost 3x for my file. I have a complicated vector-drawing colorful file with 3.5 MB size that became 10 MB!
  • Joshua Grosso Reinstate CMs
    Joshua Grosso Reinstate CMs over 6 years
    Really, is there anything ImageMagick can't do? :)
  • user1338062
    user1338062 almost 6 years
    FWIW, pdftk can concatenate PDFs, too.
  • Stanimir Stoyanov
    Stanimir Stoyanov almost 6 years
    -density 400 -quality 100 creates HUGE files. +1 for @goyinux's solution.
  • rew
    rew over 5 years
    The big files print great (only using black toner) while the original and gs-converted file use all toners and create a vague mess on my printer.
  • Mpunkt Moeniac
    Mpunkt Moeniac about 5 years
    What's the reason for convert to reduce the quality of rasterized images? I tried using the options convert -density 300 -quality 100, in order to prevent this behaviour of convert. But the quality of the output image is still much worse than the quality of the input image... so are there options for convert, that prevent the encapsulated images from quality loss, when converting pdf'-files encapsulating rasterized images?
  • xaratustra
    xaratustra about 4 years
    This is really fantastic. Perfect for scanned documents for subsequent print.
  • loved.by.Jesus
    loved.by.Jesus over 3 years
    Another problem with convert is that OCRed pdf files lose the text layer. That is, one loses the OCR. While the method with gs keeps OCR.
  • loved.by.Jesus
    loved.by.Jesus over 3 years
    Some people complains that the size of the grayscale image is not lower than the original. If, besides turning the image grayscale, you want to compress it, add the following option -dPDFSETTINGS=/ebook . Instead of /ebook you can also use /screen for a more radical compression (i.e. lower quality). Enjoy it!
  • Jean Carlo Machado
    Jean Carlo Machado about 3 years
    convert -density 200 -colorspace GRAY worked like a charm for me