ghostscript

Images are compressed when they should not be


I'm using Ghostscript to convert EPS images into PDF. We have EPS images with embedded pixel images. The conversion leaves these images at their original resolution, but it adds compression artifacts.

enter image description here

These are the parameters I use with Ghostscript:

-q -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dEPSCrop -dCompatibilityLevel=1.4 -dAutoRotatePages=/None -dDownsampleColorImages=false -dDownsampleGrayImages=false -dDownsampleMonoImages=false -dCompressPages=false -ddCompressEntireFile=false

Adding -dDownsampleColorImages=false made no difference.

Adding -dPDFSETTINGS="/printer" gives a small improvement.

I checked the various dPDFSETTINGS. -dPDFSETTINGS="/default" should be the best: it has set all downsampling options to false. But rendering the PDF with this option still gives me compression artefacts.

This is the list of options for -dPDFSETTINGS="/default". I don't see any settings for compressing images.

/AutoRotatePages /PageByPage
/CannotEmbedFontPolicy /Warning
/ColorACSImageDict << /QFactor 0.9 /Blend 1 /HSamples [2 1 1 2] /VSamples [2 1 1 2] >>
/ColorConversionStrategy /LeaveColorUnchanged
/CreateJobTicket false
/DoThumbnails false
/DownsampleColorImages false
/DownsampleGrayImages false
/DownsampleMonoImages false
/EmbedAllFonts true
/GrayACSImageDict << /QFactor 0.9 /Blend 1 /HSamples [2 1 1 2] /VSamples [2 1 1 2] >>
/NeverEmbed [/Courier /Courier-Bold /Courier-Oblique /Courier-BoldOblique /Helvetica /Helvetica-Bold /Helvetica-Oblique /Helvetica-BoldOblique /Times-Roman /Times-Bold /Times-Italic /Times-BoldItalic /Symbol /ZapfDingbats]
/Optimize false
/PreserveEPSInfo true
/PreserveOPIComments true
/PreserveOverprintSettings true
/UCRandBGInfo /Preserve

Is there a way to force Ghostscript to use lossless compression only?


Solution

  • Apparently, GS will compress raster images using JPEG compression by default.

    Compression can be disabled completely with the -dEncodeColorImages=false switch.

    But it's also possible to specify lossless compression: -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode will compress the images using Flate encoding. This gave me the result I wanted.

    -dColorImageFilter=/FlateEncode needs the / before the setting.

    If you don't want lossless compression, but higher-quality JPEG compression, you have to use Acrobat Distiller settings with the setdistillerparams option.

    This option has the syntax -c "<<... >> setdistillerparams" This option must be placed after all other Ghostscript command line parameters except for the input file. Use the -f switch to specify the input file. Insert the Distiller parameters you want to use between the << and >>.

    The option takes any Acrobat Distiller parameters as input. If you have Acrobat Distiller installed, find a *.joboptions file for examples.

    Some options are straightforward:

      /LockDistillerParams false
      /MaxSubsetPct 100
    

    these can be inserted as you'd expect.

    -c "<< /LockDistillerParams false /MaxSubsetPct 100 >> setdistillerparams"

    Some options use a dictionary. In the .joboptions file these are split into multiple lines.

    /ColorACSImageDict <<
    /QFactor 0.15
    /HSamples [1 1 1 1] /VSamples [1 1 1 1]
    >>
    

    You can paste the dictionary into the -c argument. Remove the returns.

    -c "<</ColorACSImageDict << /QFactor 0.2 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >>>> setdistillerparams"
    

    Full command line:

    "C:\Program Files\gs\gs9.52\bin\gswin64c.exe" -q -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dEPSCrop -dCompatibilityLevel=1.4 -sOutputFile=testfile.pdf  -c "<</ColorACSImageDict << /QFactor 0.2 /HSamples [1 1 1 1] /VSamples [1 1 1 1] >>>> setdistillerparams" -f testfile.eps