title: How to limit PDF rasterization size when using ImageMagick/Ghostscript
body: <p>To convert pdfs/eps to pixel images we use ImageMagick. When converting a PDF the command can look like this:</p>
  
  <pre><code>convert -verbose -density 150 -trim -colorspace sRGB input.pdf -quality 90 -flatten -sharpen 0x1.0 output.png
  </code></pre>
  
  <p>However, the input PDF (which contains only a few paths) has a specified size of 300cm by 200cm, and Ghostscript doesn't like this, and creates a huge png. The verbose output of imagemagick shows this:</p>
  
  <pre><code>"gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r150x150"  "-sOutputFile=/tmp/magick-3036AW7mUOP25w7J%d" "-f/tmp/magick-3036PxgJinljqMwV" "-f/tmp/magick-30369hcErAROr7V6"
  /tmp/magick-3036AW7mUOP25w7J1 PNG 17717x11811 17717x11811+0+0 8-bit sRGB 1.003MB 3.910u 0:03.929
  input.pdf PNG 17717x11811 17717x11811+0+0 16-bit sRGB 1.003MB 0.000u 0:00.000
  </code></pre>
  
  <p><em>PNG 17717x11811.</em> Huge. I only need a 256x256 image.</p>
  
  <p>I have tried the geometry (-geometry 256x256) options and the density option (before the filename, -density 150) in different configurations, but I does not change the ghostscript output, for example:</p>
  
  <pre><code>convert -verbose -density 150 -trim -geometry 265x265 -colorspace sRGB input.pdf -quality 90 output.png
  "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r150x150"  "-sOutputFile=/tmp/magick-3233p3ofct0fiy5T%d" "-f/tmp/magick-3233Rads_vSSpKa6" "-f/tmp/magick-3233LQMrrEFgT0fi"
  /tmp/magick-3233p3ofct0fiy5T1 PNG 17717x11811 17717x11811+0+0 8-bit sRGB 1.003MB 3.800u 0:03.799
  input.pdf PNG 17717x11811 17717x11811+0+0 16-bit sRGB 1.003MB 0.000u 0:00.000
  input.pdf=&gt;output.png PNG 17717x11811=&gt;265x38 321x213+28+65 16-bit sRGB 7.96KB 1.080u 0:00.559
  </code></pre>
  
  <p>How can you define the constraints for Ghostscript, when using ImageMagick?</p>