title: Ghostscript grayscale conversion still contains colors?
body: <p>I need to convert a pdf in grayscale if it does contain colors.
  For this purpose i found a script which can determine if the pdf is already in grayscale or not.</p>
  
  <pre><code>convert "source.pdf" -colorspace RGB -unique-colors txt:- 2&gt; /dev/null \
     | egrep -m 2 -v "#([0-9|A-F][0-9|A-F])\1{3}" \
     | wc -l
  </code></pre>
  
  <p>This counts how many colors with different values of RGB (so they are not gray) are present in the document.</p>
  
  <p>If the pdf is not already a grayscale document i proceed with the conversion with ghostscript</p>
  
  <pre><code>gs \
    -sOutputFile=temp.pdf \
    -sDEVICE=pdfwrite \
    -sColorConversionStrategy=Gray \
    -dProcessColorModel=/DeviceGray \
    -dCompatibilityLevel=1.4 \
    -dNOPAUSE \
    -dBATCH \
     source.pdf &lt; /dev/null
  </code></pre>
  
  <p>If i open the output document with a PDF viewer it shows without colors correctly. But if i try the first script on the new generated document it turns out that it still does contain some colors. How can i convert a document to precise grayscale? I need this because if i print this document with a color printer, the printer will use colors and not black to print gray.</p>