From 0029cc4fe75214f7ab4a356646286becff1a19d3 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Fri, 29 Jun 2018 00:09:20 -0700 Subject: [PATCH] optimize: fix PNGs that were reduced to 1-bit being inverted At some point the color gets flipped, we have to flip it again, for mono. Incidentally this exposed an unused optimization. Should change the first past to scan all images and record monochrome xrefs, then optimize JPEG and PNG, possibly adding mono images to the monochrome queue. Finally, do JBIG2 optimization. --- src/ocrmypdf/leptonica.py | 3 +++ src/ocrmypdf/optimize.py | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/ocrmypdf/leptonica.py b/src/ocrmypdf/leptonica.py index 50fdb3b1..9f7e4b40 100644 --- a/src/ocrmypdf/leptonica.py +++ b/src/ocrmypdf/leptonica.py @@ -496,6 +496,9 @@ class Pix: raise LeptonicaError("Generate PDF data failed") return CompressedData(p_compdata[0]) + def invert(self): + return Pix(lept.pixInvert(ffi.NULL, self._pix)) + @staticmethod def _pix_destroy(pix): p_pix = ffi.new('PIX **', pix) diff --git a/src/ocrmypdf/optimize.py b/src/ocrmypdf/optimize.py index 916e92c6..4af75f16 100644 --- a/src/ocrmypdf/optimize.py +++ b/src/ocrmypdf/optimize.py @@ -261,6 +261,8 @@ def transcode_pngs(pike, pngs, root, log, options): # Open, transcode (!), package for PDF try: pix = leptonica.Pix.open(png_name(root, xref)) + if pix.depth == 1: + pix = pix.invert() # PDF assumes 1 is black for monochrome compdata = pix.generate_pdf_ci_data( leptonica.lept.L_FLATE_ENCODE, 0 )