Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ghostscript creating huge images, order of magnitude more pixels than expected

Tags:

ghostscript

I use this Ghostscript command to convert a PDF to images:

gs \
 -q -dQUIET -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT \
 -dCOLORSCREEN \
 -dDOINTERPOLATE \
 -dMaxBitmap=500000000 \
 -dNumRenderingThreads=4 \
 -dAlignToPixels=0 \
 -dGridFitTT=0 \
 -dTextAlphaBits=4 \
 -dGraphicsAlphaBits=4 \
 -sDEVICE=jpeg \
 -dFirstPage=1 \
 -dDEVICEWIDTHPOINTS=1654 \
 -dDEVICEHEIGHTPOINTS=2333 \
 -dDEVICEXRESOLUTION=1654 \
 -dDEVICEYRESOLUTION=2333 \
 -sOutputFile=d:\images\%01d.jpg \
  d:\test.pdf

It converts the images, but the images are 13675x27280px. I want the images in 1654x2333px. What am I doing wrong?

like image 759
Martin Avatar asked Dec 09 '22 05:12

Martin


1 Answers

You have specified the width and height of the media in PostScript points, of which there are 72 to the inch, and then specified a huge resolution (1654x2333) defined in dots per inch/

The size of the media is only proportional to the size in pixels of the output, because that also depends on the resolution. If I set the media to 72 points (1 inch) and a resolution of 300 dpi then the resulting image will be 300 pixels. If I set it to 600 dpi then the image will be 600 pixels.

So you have said the media is 1654 points (22.97222 inches) by 2333 points (32.402777 inches. You have then said the resolution in the x direction is 1654 dpi. So the size (in pixels) in the x-direction is 22.97222 * 1654 = 37966.055. Similar calculations give the y size as 75596.68.

Although this is not the same as the size you state, this is probably because you haven't set -dFIXEDMEDIA, so the media size in the PDF file is overriding the media size supplied on the command line. Taking the resolution and size this means that the media width is 13675/1654 = 8.25 inches and the height is 11.69 inches. Your PDF file was originally using A4 as the page size.

You will need to specify a resolution and page size which when calculated together give you the size in pixels that you want. You will also have to set -dFIXEDMEDIA to prevent the PDF file overriding the specified size.

Finally you will probably want to specify -dPDFFITPAGE so that the PDF is scaled to fit the media, assuming that is what you actually want to happen.

In this case, simply setting a resolution of 200 dpi, and ignoring the other points, will give you a result close to what you want (-r200). Obviosly you will have to remove the -dDEVICERESOLUTION switches.

like image 161
KenS Avatar answered Apr 09 '23 14:04

KenS