Specify the JPEG image quality
Leave feedback
Decreasing the JPG images quality reduces the size of the output file.
To adjust images quality, enable the compress_images property and set the image_quality property of the PdfOptimizationOptions class. The value must be between 1 (minimum quality) and 100. The default value is 90. Without compress_images, the quality has no effect.
To set the image quality, follow these steps:
- Instantiate the Viewer class. Specify the source document path as a constructor parameter.
- Instantiate the PdfViewOptions object.
- Set the PdfViewOptions.pdf_optimization_options value as instantiated PdfOptimizationOptions object.
- Set the PdfViewOptions.pdf_optimization_options.compress_images value to
Trueand the PdfViewOptions.pdf_optimization_options.image_quality value. - Call the view method of the Viewer object. Specify the PdfViewOptions object as the parameter.
The following code snippet shows how to adjust JPG image quality in the output PDF document:
from groupdocs.viewer import Viewer
from groupdocs.viewer.options import PdfViewOptions, PdfOptimizationOptions
def adjust_jpeg_quality():
# Load document
with Viewer("sample.docx") as viewer:
# Create view options.
viewOptions = PdfViewOptions("adjust_jpeg_quality/optimized_jpeg_quality.pdf")
# Specify the JPG image quality.
pdf_optimization_options = PdfOptimizationOptions()
pdf_optimization_options.compress_images = True
pdf_optimization_options.image_quality = 50
# Specify pdf_optimization_options object
viewOptions.pdf_optimization_options = pdf_optimization_options
viewer.view(viewOptions)
if __name__ == "__main__":
adjust_jpeg_quality()
sample.docx is the sample file used in this example. Click here to download it.
Binary file (PDF, 483 KB)
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.