Does grayscale conversion in Ghostscript still contain colors?

I need to convert a PDF to shades of gray if it contains colors. For this, I found a script that can determine if a PDF file is in grayscale or not.

convert "source.pdf" -colorspace RGB -unique-colors txt:- 2> /dev/null \
   | egrep -m 2 -v "#([0-9|A-F][0-9|A-F])\1{3}" \
   | wc -l

This counts how many colors with different RGB values ​​(therefore they are not gray) are present in the document.

If the PDF file is not yet grayscale, I proceed to conversion using ghostscript

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

PDF, . script , , . ? , , , , .

+4
2

ImageMagick, convert , ...

, , PDF ? ( ) Ghostscript inkcov ( Ghostscript v9.05 ). CMYK ( RGB, CMYK ).

PDF Ghostscript:

gs \
  -o test.pdf \
  -sDEVICE=pdfwrite \
  -g5950x2105 \
  -c "/F1 {100 100 moveto /Helvetica findfont 42 scalefont setfont} def" \
  -c "F1                         (100% 'pure' black)   show showpage" \
  -c "F1 .5 .5 .5   setrgbcolor  (50% 'rich' rgbgray)  show showpage" \
  -c "F1 .5 .5 .5 0 setcmykcolor (50% 'rich' cmykgray) show showpage" \
  -c "F1 .5         setgray      (50% 'pure' gray)     show showpage"

, - , 2 3 .

:

gs  -o - -sDEVICE=inkcov test.pdf 
 [...]
 Page 1
 0.00000  0.00000  0.00000  0.02230 CMYK OK
 Page 2
 0.02360  0.02360  0.02360  0.02360 CMYK OK
 Page 3
 0.02525  0.02525  0.02525  0.00000 CMYK OK
 Page 4
 0.00000  0.00000  0.00000  0.01982 CMYK OK

( 1.00000 100% . , 0.02230 , 2.23 % .) , Ghostscript inkcov :

  • 1 + 4 C (), M (), Y (), K ().
  • 2 + 3 C (), M (), Y (), K () .

PDF, DeviceGray:

gs \
 -o temp.pdf \
 -sDEVICE=pdfwrite \
 -sColorConversionStrategy=Gray \
 -sProcessColorModel=DeviceGray \
  test.pdf

... :

gs -q  -o - -sDEVICE=inkcov temp.pdf
 0.00000  0.00000  0.00000  0.02230 CMYK OK
 0.00000  0.00000  0.00000  0.02360 CMYK OK
 0.00000  0.00000  0.00000  0.02525 CMYK OK
 0.00000  0.00000  0.00000  0.01982 CMYK OK

, ! (BTW, convert 2 , [] test.pdf, [ ] temp.pdf - ...)

+9

, .

-dHaveTransparency=false

ghostscript. pdfwrite http://ghostscript.com/doc/current/Ps2pdf.htm#Options

+4

All Articles