admin管理员组

文章数量:1122826

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.

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?

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.

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?

Share Improve this question edited Nov 21, 2024 at 14:24 Hobbes asked Nov 21, 2024 at 11:24 HobbesHobbes 2,0833 gold badges20 silver badges39 bronze badges 7
  • You need to use ColorImageFilter and set it to /Flate, and also set AutoFilterColorImages to false. The various settings are (mostly) controlled via setdistillerparams, as you note in your partial answer below. DownsampleColorImages is nothing to do with compression, it reduces the effective resolution of images. There is no switch -ddCompressEntireFile, if you remove the duplicate 'd' then there is, but it only works with the ps2write device, not the pdfwrite device. – KenS Commented Nov 21, 2024 at 17:02
  • After writing the answer, I ran into the ColorImageFilter option. I was unable to get it to work though: with ColorImageFilter set to Flate, I still got an image with lossy compression instead of the lossless compression I expected. I'll do more tests tomorrow. – Hobbes Commented Nov 21, 2024 at 17:57
  • 1 The correct command is -dColorImageFilter=/FlateEncode. I was using -dColorImageFilter=FlateEncode which gives a misleading error message "use -sNAME= to define string constants" suggesting I should use -sNamedColorImageFilter=/FlateEncode, which silently fails, using JPEG compression instead of Flate. – Hobbes Commented Nov 25, 2024 at 10:09
  • If you are unsure what type of (command line) parameter to use when setting the 'distiller parameters' then use 'setdistillerparams' instead, which takes a PostScript dictionary, there can be no confusion there. The -s didn't silently fail, it succeeded, but that simply sets a string parameter, when the parameter ought to be a name. Because of the way command line parsing works in Ghostscript, setting a non-existent parameter is not an error. – KenS Commented Dec 3, 2024 at 9:46
  • Using setdistillerparams just moves the problem I had. The problem is that I don't know which syntax to use and the help files aren't helpful. The exact same thing goes for setdistillerparams. "C:\Program Files\gs\gs9.52\bin\gswin64c.exe" -q -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dEPSCrop -dCompatibilityLevel=1.4 -sOutputFile=91434.pdf 91434.eps -c "<</ColorImageFilter /FlateEncode >> setdistillerparams" creates a JPEG-compressed file while I expect a Flate-compressed file that is 3 times larger. – Hobbes Commented Dec 4, 2024 at 14:09
 |  Show 2 more comments

1 Answer 1

Reset to default 1

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

本文标签: ghostscriptImages are compressed when they should not beStack Overflow