Ghostscript: Quality and Size issue

18,079

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

  1. find the width and the height in points (%%BounDingBox)
  2. 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

Share:
18,079

Related videos on Youtube

hofnarwillie
Author by

hofnarwillie

Updated on June 04, 2022

Comments

  • hofnarwillie
    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
    hofnarwillie almost 11 years
    Why 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
    Frank Rem almost 11 years
    Ah... 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
    hofnarwillie almost 11 years
    Nope, original documents are scanned in pages
  • KenS
    KenS almost 11 years
    We can't reasonably comment without seeing your original PDF file.
  • Frank Rem
    Frank Rem almost 11 years
    I 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
    hofnarwillie almost 11 years
    @FrankRem Can you explain what you mean by extracting as opposed to converting?
  • Frank Rem
    Frank Rem almost 11 years
    With 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
    maliayas over 8 years
    Important to note that dDownScaleFactor is only supported for PNG format, at least until GS 9.18
  • Wolf5
    Wolf5 over 6 years
    Using 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.