From 93f1b73579827479d20fc618ad2540e2ff5374e7 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 5 Jun 2019 02:04:45 -0700 Subject: [PATCH] Fix --remove-vectors which was broken in API migration It got dropped during the change. This feature has also been altered so that the final visual appearance of the file is not affected, only the OCR image. --- src/ocrmypdf/_pipeline.py | 14 ++++++--- src/ocrmypdf/_sync.py | 65 ++++++++++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/ocrmypdf/_pipeline.py b/src/ocrmypdf/_pipeline.py index 189e6fbc..c09c31f1 100644 --- a/src/ocrmypdf/_pipeline.py +++ b/src/ocrmypdf/_pipeline.py @@ -392,10 +392,16 @@ def get_orientation_correction(preview, page_context): return 0 -def rasterize(input_file, page_context, correction=0): +def rasterize( + input_file, page_context, correction=0, output_tag='', remove_vectors=None +): colorspaces = ['pngmono', 'pnggray', 'png256', 'png16m'] device_idx = 0 - output_file = page_context.get_path('rasterize.png') + + if remove_vectors is None: + remove_vectors = page_context.options.remove_vectors + + output_file = page_context.get_path(f'rasterize{output_tag}.png') pageinfo = page_context.pageinfo def at_least(cs): @@ -431,7 +437,7 @@ def rasterize(input_file, page_context, correction=0): page_dpi=(page_dpi, page_dpi), pageno=pageinfo.pageno + 1, rotation=correction, - filter_vector=page_context.options.remove_vectors, + filter_vector=remove_vectors, ) return output_file @@ -520,7 +526,7 @@ def create_ocr_image(image, page_context): barcodes = pix.locate_barcodes() for barcode in barcodes: decoded, rect = barcode - print('masking barcode %s %r', decoded, rect) + page_context.log.debug('masking barcode %s %r', decoded, rect) draw.rectangle(rect, fill=white) im = pix.topil() diff --git a/src/ocrmypdf/_sync.py b/src/ocrmypdf/_sync.py index 62fc5a39..1ca758d1 100644 --- a/src/ocrmypdf/_sync.py +++ b/src/ocrmypdf/_sync.py @@ -73,6 +73,16 @@ PageResult = namedtuple( ) +def preprocess(page_context, image, remove_background, deskew, clean): + if remove_background: + image = preprocess_remove_background(image, page_context) + if deskew: + image = preprocess_deskew(image, page_context) + if clean: + image = preprocess_clean(image, page_context) + return image + + def exec_page_sync(page_context): options = page_context.options orientation_correction = 0 @@ -88,27 +98,46 @@ def exec_page_sync(page_context): ) rasterize_out = rasterize( - page_context.origin, page_context, correction=orientation_correction + page_context.origin, + page_context, + correction=orientation_correction, + remove_vectors=False, ) - preprocess = rasterize_out - if options.remove_background: - preprocess = preprocess_remove_background(preprocess, page_context) - - if options.deskew: - preprocess = preprocess_deskew(preprocess, page_context) - - if options.clean: - cleaned = preprocess_clean(preprocess, page_context) - if options.clean_final: - preprocess_out = cleaned - ocr_image = cleaned - else: - preprocess_out = preprocess - ocr_image = cleaned + if not any([options.clean, options.clean_final, options.remove_vectors]): + ocr_image = preprocess_out = preprocess( + page_context, + rasterize_out, + options.remove_background, + options.deskew, + clean=False, + ) else: - preprocess_out = preprocess - ocr_image = preprocess + if not options.lossless_reconstruction: + preprocess_out = preprocess( + page_context, + rasterize_out, + options.remove_background, + options.deskew, + clean=options.clean_final, + ) + if options.remove_vectors: + rasterize_ocr_out = rasterize( + page_context.origin, + page_context, + correction=orientation_correction, + remove_vectors=True, + output_tag='_ocr', + ) + else: + rasterize_ocr_out = rasterize_out + ocr_image = preprocess( + page_context, + rasterize_ocr_out, + options.remove_background, + options.deskew, + clean=options.clean, + ) ocr_image_out = create_ocr_image(ocr_image, page_context)