# Is there a way to convert an image to grayscale?

Is there a way to convert a colored image to grayscale both for printing as well as screen viewing through pdfLaTeX?

-
if you run the latex->dvips->ps2pdf sequence it can be done from within latex –  Herbert Sep 20 '11 at 8:07
@Herbert Thanks! I was always sure there was a way but could not figure it. Also I still have the gut feeling that one could hack straight into the pdf. –  Yiannis Lazarides Sep 20 '11 at 13:35

I would suggest using a tool like "convert" of the ImageMagick package (which is usually installed if you're running Linux, but is also available for Windows).

convert -colorspace gray originalfile outputfile
-
Of course, this is a preprocessing step, and not something done by pdfLaTeX as such. –  plc Sep 20 '10 at 20:18
exactly, I was looking for a way through LaTeX –  Yiannis Lazarides Sep 21 '10 at 2:02
There is no color ocnversion code inside pdftex, but if \write18 is enabled, you could hook the external conversion into the compilation process transparently. I assume the graphicx package manual will explain how that is to be done? –  Taco Hoekwater Sep 21 '10 at 7:09
Unfortunately, convert only works with raster images. Used with PDF (or any vector format) it produces poor results. –  Konrad Rudolph Sep 21 '10 at 10:04
@taco thanks will enable and give it a try. –  Yiannis Lazarides Sep 21 '10 at 10:29

Just a note on this, in respect to Linux tools:

• convert can convert color (mode) of bitmap/raster images to grayscale (it can also convert a pdf to a bitmap/raster image)
• ghostscript can convert pdfs (that include images) into grayscale pdfs (see comments in graypdf.sh, though; there's problems with metadata)
• options monochrome to (x)color for pdflatex will cause mostly text color (and, I guess, related vector objects) to be grayscale

So, basically there would be two approaches to generating a gray pdflatex document:

• pdflatex it in color - and then convert the whole document to gray using ghostscript
• Use pdflatex with \write18, and use definitions of commands that will generate grayscale image on the fly.

Note that even if converting the whole PDF, the process will not necessarily be straightforward (for more, see Converting (any) PDF to black (K)-only CMYK).

For the second option, there is an example of a \includegraphicsRS in:

, which calls convert under \write18 to generate resized versions of images; I'm guessing it can be relatively easily modified to generate grayscale images instead (it may also be possible to force convert to return the image data through stdout - so it is directly embedded in the pdf stream, instead of generating an intermediate file; but I wouldn't know how to solve that at the moment).

Anyways, hope this helps someone,
Cheers!

-