Ghostscript: Quality and Size issue
Solution 1
Got it! Added the following parameter to my GS command:
-dDownScaleFactor=3
From the GS documentation:
This causes the internal rendering to be scaled down by the given (small integer) factor before being output. For example, the following will produce a 200dpi output png from a 300dpi internal rendering:
gs -sDEVICE=png16m -r600 -dDownScaleFactor=3 -o tiger.png\ examples/tiger.png
Solution 2
I had a similar problem, where PDF conversion to PNG using ghostscript resulted in an image with much greater dimensions (including extra white space). I solved the issue by using
-dUseCropBox
... which sets the page size to the CropBox rather than the MediaBox
Solution 3
The quality-size tradeoff is inevitable. You may choose a different compression to keep size down while maintaining reasonable quality. E.g. DCT (jpeg) or jpeg2000 if your content mainly consists of photographic images or CCITT or JBIG2 if your content is mainly black and white.
Solution 4
- find the width and the height in points (%%BounDingBox)
use them
gs -sDEVICE=png16m -dDEVICEWIDTHPOINTS=$l -dDEVICEHEIGHTPOINTS=$h -r600 -dDownScaleFactor=3 -o tiger.png\ examples/tiger.png
where $w
is the width and $h
the height
Related videos on Youtube
hofnarwillie
Updated on June 04, 2022Comments
-
hofnarwillie almost 2 years
I have a ghostscript command that converts a pdf into several PNG images (one for every page). The command arguments are as follows:
-dNOPAUSE -q -r300 -sPAPERSIZE=a4 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dUseTrimBox -sDEVICE=png16m -dBATCH -sOutputFile="C:\outputfile%d.png" -c \"30000000 setvmthreshold\" -f "C:\inputfile.pdf"
The pdf displays as regular A4 pages in Adobe Reader, but in the PNG images it becomes huge (2480 by 3507 pixels for instance).
if I change the resolution in the ghostscript command to
-r110
the page size is correct but the image quality is very rastorized.Is there another way to improve the quality of the image without affecting the image size?
Thanks
-
hofnarwillie almost 11 yearsWhy is this? I get the concept, but I only want to keep the size and quality the same as the original PDF. It's not like I'm increasing the size?
-
Frank Rem almost 11 yearsAh... likely, it is because your original PDF consists of vector graphics. In general, these take few storage compared to raster data that approximate the original graphics.
-
hofnarwillie almost 11 yearsNope, original documents are scanned in pages
-
KenS almost 11 yearsWe can't reasonably comment without seeing your original PDF file.
-
Frank Rem almost 11 yearsI agree with Ken. (If your documents are scans, then it should be possible to achieve same size, quality. But it would be more like extracting as opposed to converting.)
-
hofnarwillie almost 11 years@FrankRem Can you explain what you mean by extracting as opposed to converting?
-
Frank Rem almost 11 yearsWith extract I mean take the embedded image data and save it as an image file without touching the image data. Normally this involes wrapping the image in the appropriate image file format (prepend header, etc.). Drawing in general involves fitting the embedded image data to the output raster involving upscaling or downscaling, interpolating..
-
maliayas over 8 yearsImportant to note that
dDownScaleFactor
is only supported for PNG format, at least until GS 9.18 -
Wolf5 over 6 yearsUsing that option fixed a few conversions where the actual image became a small thumb in the left bottom corner and the rest of the image white space.