Some test text!

< Linux samples

PDF Image JBIG2 Compression in Python (Lossy & Lossless)

Sample Python code for using PDFTron SDK to recompress bitonal (black and white) images in existing PDF documents using JBIG2 compression (lossless or lossy). The sample is intended to show how to specify hint information for the image encoder and is not meant to be a generic PDF optimization tool. To demonstrate the possible compression rates, we recompressed a document containing 17 scanned pages. The original input document is ~1.4MB and is using standard CCITT Fax compression. Lossless JBIG2 compression shrunk the filesize to 641KB, while lossy JBIG2 compression shrunk it to 176KB.

Step 1: Get your free trial license key, or sign in

Start Trial
Sign in

Step 2: Add the code:

# Copyright (c) 2001-2018 by PDFTron Systems Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.

import site
import sys
from PDFNetPython import *

# This sample project illustrates how to recompress bi-tonal images in an 
# existing PDF document using JBIG2 compression. The sample is not intended 
# to be a generic PDF optimization tool.
# You can download the entire document using the following link:

def main():
    pdf_doc = PDFDoc("../../TestFiles/US061222892-a.pdf")
    cos_doc = pdf_doc.GetSDFDoc()
    num_objs = cos_doc.XRefSize()
    i = 1
    while i < num_objs:
        obj = cos_doc.GetObj(i)
        if obj is not None and not obj.IsFree() and obj.IsStream():
            # Process only images
            itr = obj.Find("Subtype")
            if not itr.HasNext() or not itr.Value().GetName() == "Image":
                i = i + 1
            input_image = Image(obj)
            # Process only gray-scale images
            if input_image.GetComponentNum() != 1:
                i = i + 1
            # Skip images that are already compressed using JBIG2
            itr = obj.Find("Filter")
            if (itr.HasNext() and itr.Value().IsName() and itr.Value().GetName() == "JBIG2Decode"):
                i = i + 1
            filter = obj.GetDecodedStream()
            reader = FilterReader(filter)
            hint_set = ObjSet()     # hint to image encoder to use JBIG2 compression
            hint = hint_set.CreateArray()
            new_image = (Image.Create(cos_doc, reader, 
            new_img_obj = new_image.GetSDFObj()
            itr = obj.Find("Decode")
            if itr.HasNext():
                new_img_obj.Put("Decode", itr.Value())
            itr = obj.Find("ImageMask")
            if itr.HasNext():
                new_img_obj.Put("ImageMask", itr.Value())
            itr = obj.Find("Mask")
            if itr.HasNext():
                new_img_obj.Put("Mask", itr.Value())
            cos_doc.Swap(i, new_img_obj.GetObjNum())
        i = i + 1
    pdf_doc.Save("../../TestFiles/Output/US061222892_JBIG2.pdf", SDFDoc.e_remove_unused)

if __name__ == '__main__':