tribol
tribol

Reputation: 365

ghostscript pdfwrite specify jpeg quality

I'm trying to concatenate multiple pdf files which basically are the pages of a photobook containing jpg images. For my output pdf file I wish to adjust the image resolution to 300 dpi and I want to keep the best quality. The commands I'm using are:

gswin64c.exe -dNOPAUSE -dBATCH ^-dDownsampleColorImages=true -dColorImageResolution=300 ^-dDownsampleGrayImages=true -dGrayImageResolution=300 ^-dDownsampleMonoImages=true -dMonoImageResolution=300 ^-sDEVICE=pdfwrite -dJPEGQ=100 -sOutputFile=out.pdf in1.pdf in2.pdf

However, it seems that -dJPEGQ=100 has no effect on the output. Changing this parameter leads to the same filesize and artifacts are visible in the images for all values. Running the command with the option -dPDFSETTINGS=/printer I get better results without artifacts, however this option should also result in 300 dpi. So what is the correct command to specify the quality of the jpg images in the output file?

Upvotes: 9

Views: 7979

Answers (2)

jtchen2k
jtchen2k

Reputation: 319

Ghostscript seems to have issues reading the /QFactor specified in the -c "..." part for some files. I have tried using this minimal command on my pdf generated by pdflatex for testing:

gs -sDEVICE=pdfwrite -sOutputFile=output.pdf \
    -dCompatibilityLevel=1.7 -dNOPAUSE -dBATCH \
    -c '<< /ColorACSImageDict << /VSamples [ 1 1 1 1 ] /HSamples [ 1 1 1 1 ] /QFactor 0.15 /Blend 1 /ColorTransform 1 >> >> setdistillerparams ' \
    -f input.pdf

I find that no matter what /QFactor is specified, the file size and image quality of the output.pdf is the same. I think it is still using the default /QFactor value, which is 0.9.

So I think the only way to specify JPEG quality for pdfwrite is to use a predefined -dPDFSETTINGS to set some QFactor value, and use additional flags to override other settings. The QFactor values for default, ebook, printer and prepress are 0.9, 0.76, 0.4, and 0.15, respectively. See gs document here: https://www.ghostscript.com/~chrisl/staging/ghostpdl/doc/_build/VectorDevices.html

Here's what I use for my PDF workflow. (The QFactor of 0.25 doesn't really work though):

DPI=600
gs -sDEVICE=pdfwrite -sOutputFile=output.pdf \
    -dCompatibilityLevel=1.7 -dFastWebView=true \
    -dEmbedAllFonts=true -dSubsetFonts=true -dCompressFonts=true \
    -dDetectDuplicateImages=true -dDoThumbnails=false -dAutoRotatePages=/None \
    -r$DPI -dColorImageResolution=$DPI -dGrayImageResolution=$DPI -dMonoImageResolution=$DPI \
    -dDownsampleColorImages=true -dColorImageDownsampleType=/Bicubic -dColorImageDownsampleThreshold=1.0 \
    -dDownsampleGrayImages=true -dGrayImageDownsampleType=/Bicubic -dGrayImageDownsampleThreshold=1.0 \
    -dDownsampleMonoImages=true -dMonoImageDownsampleType=/Subsample -dMonoImageDownsampleThreshold=1.0 \
    -dColorImageFilter=/DCTEncode -dGrayImageFilter=/DCTEncode -dMonoImageFilter=/CCITTFaxEncode \
    -dColorConversionStrategy=/LeaveColorUnchanged -dPassThroughJPEGImages=true \
    -dUseArtBox -dNEWPDF=true -dNOPAUSE -dBATCH -dSAFER \
    -dPDFSETTINGS=/printer \
    -c '<< /ColorACSImageDict << /VSamples [ 1 1 1 1 ] /HSamples [ 1 1 1 1 ] /QFactor 0.25 /Blend 1 /ColorTransform 1 >> >> setdistillerparams ' \
    -f input.pdf

Upvotes: 3

tribol
tribol

Reputation: 365

The solution is adjusting the DCTEncode filter with follwing commands:

gswin64c.exe -sOutputFile=out.pdf -dNOPAUSE -dBATCH ^-sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -c "<< /ColorACSImageDict << /VSamples [ 1 1 1 1 ] /HSamples [ 1 1 1 1 ] /QFactor 0.08 /Blend 1 >> /ColorImageDownsampleType /Bicubic /ColorConversionStrategy /LeaveColorUnchanged >> setdistillerparams" -f in1.pdf

which lead to a compressed file with satisfactory quality for me and can be adjusted to each individual needs.

Edit:

the .setpdfwrite argument is deprecated for recent ghostscript releases (> 9.50), so I removed it in the answer

Upvotes: 8

Related Questions