-
Notifications
You must be signed in to change notification settings - Fork 0
/
OCRmyPDF_DjVu_Optimize
executable file
·427 lines (345 loc) · 12.9 KB
/
OCRmyPDF_DjVu_Optimize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/python3
# OCRmyPDF Optimizatopm plugin by Robert Mast inspired by James R. Barlow: github.com/jbarlow83, Merlijn Wajer <merlijn@archive.org>
# enabling OCRmyPDF to optimize PDF's as good as Open Sourced parts of DjVu can do.
# As some DjVu-software-patents have expired there might even be some additional room for improvement for anyone who is able to understand them deeply.
"""Built-in plugin to implement PDF page optimization."""
import itertools
import logging
import os
import sys
import importlib
import argparse
from pathlib import Path
from typing import (
Callable,
Dict,
Iterator,
List,
MutableSet,
NamedTuple,
NewType,
Optional,
Sequence,
Tuple,
)
from ocrmypdf import PdfContext, hookimpl
from ocrmypdf._concurrent import Executor, SerialExecutor
from ocrmypdf._exec import jbig2enc, pngquant
from ocrmypdf._pipeline import get_pdf_save_settings
from ocrmypdf.cli import numeric
from ocrmypdf.optimize import optimize
from ocrmypdf.subprocess import check_external_program
from subprocess import check_call, check_output
import os.path
import tempfile
import shutil
import threading
from collections import defaultdict
from os import fspath
from zlib import compress
import img2pdf
from pikepdf import (
Dictionary,
Name,
Object,
ObjectStreamMode,
Pdf,
PdfError,
PdfImage,
Stream,
UnsupportedImageTypeError,
)
from PIL import Image
#from ocrmypdf._jobcontext import PdfContext
from ocrmypdf.exceptions import OutputFileAccessError
from ocrmypdf.helpers import IMG2PDF_KWARGS, safe_symlink
import io
import fitz
from hocr.parse import hocr_page_iterator, hocr_page_to_word_data
from internetarchivepdf.const import DENOISE_FAST, JPEG2000_IMPL_KAKADU, \
JPEG2000_IMPL_PILLOW, COMPRESSOR_JPEG, COMPRESSOR_JPEG2000
#from internetarchivepdf.mrc import create_mrc_hocr_components
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/didjvu/lib")
#sys.path.insert(0, './didjvu/lib')
from didjvu.lib import djvu_support as djvu
#from . import filetype
#from . import fs
from didjvu.lib import gamera_support as gamera
from didjvu.lib import ipc
#from . import templates
from didjvu.lib import temporary
from didjvu.lib import utils
#from . import xmp
from didjvu.lib import didjvu
from internetarchivepdf.mrc import encode_mrc_images
# TODO:
# - Deal with arbitrary rotation and matrix sizes when placing the image back
# - Decide if we want to ignore transparent images alltogether
# - Give black/white images (1bit images) special treatment
# - Stabilise and clean up the code, the many clean_contents
# -
def remove_images(doc, page, unwanted):
un_list = [b"/%s Do" % u.encode() for u in unwanted]
#page.clean_contents() # unify / format the commands
xref=page.get_contents()[0] # get its XREF
cont=page.read_contents().splitlines() # read commands as list of lines
for i in range(len(cont)): # walk thru the lines
if cont[i] in un_list: # invokes an unwanted image
cont[i] = b"" # remove command
doc.update_stream(xref, b"\n".join(cont)) # replace cleaned command object
#page.clean_contents() # removes now unreferenced images from page definition
def compress_page_images(doc, page, hocr_word_data=[]):
page.clean_contents()
imgs = page.get_images(full=True)
to_remove_xrefs = []
to_insert = []
for img_data in imgs:
xref = img_data[0]
#print(img_data)
orig_img = doc.extract_image(xref)
to_remove_xrefs.append(xref)
bbox = page.get_image_bbox(img_data)
#print(bbox)
imgfd = io.BytesIO()
imgfd.write(orig_img["image"])
pil_image = Image.open(imgfd)
pil_image.load()
# TODO: if greyscale or 1bit, treat differently
# TODO: force 1bit mode?
#print('image mode', pil_image.mode)
#print('image size', pil_image.size)
imgfd.close()
dpi = orig_img['xres']
djvu.require_cli()
numpy_store = sys.modules['numpy']
gamera.init()
sys.modules['numpy'] = numpy_store
mrc_gen = create_mrc_hocr_components(pil_image, hocr_word_data,
#mrc_gen = create_mrc_hocr_components(pil_image, [],
denoise_mask=DENOISE_FAST,
bg_downsample=3
)
fg_slope = 44500
bg_slope = 44250
# with pillow
#mask_f, bg_f, bg_s, fg_f, fg_s = encode_mrc_images(mrc_gen,
# jpeg2000_implementation=JPEG2000_IMPL_PILLOW,
# bg_compression_flags=['quality_mode:"rates";quality_layers:[250]'],
# #fg_compression_flags=['quality_mode:"rates";quality_layers:[300]'],
# fg_compression_flags=[''],
# )
# with jpegoptim
#mask_f, bg_f, bg_s, fg_f, fg_s = encode_mrc_images(mrc_gen,
# mrc_image_format=COMPRESSOR_JPEG,
# bg_compression_flags=['-S30'],
# fg_compression_flags=['-S20'],
# )
mask_f, bg_f, bg_s, fg_f, fg_s = encode_mrc_images(mrc_gen,
jpeg2000_implementation=JPEG2000_IMPL_KAKADU,
bg_compression_flags=['-slope', str(bg_slope)],
#fg_compression_flags=['-slope', str(fg_slope)],
fg_compression_flags=['-com','hoi'],
)
# TODO: maybe we can replace the existing image with the background image
# here
bg_contents = open(bg_f, 'rb').read()
fg_contents = open(fg_f, 'rb').read()
mask_contents = open(mask_f, 'rb').read()
os.remove(mask_f)
os.remove(bg_f)
os.remove(fg_f)
to_insert.append([
{'bbox': bbox, 'stream': bg_contents, 'mask': None, 'overlay': False},
{'bbox': bbox, 'stream': fg_contents, 'mask': mask_contents, 'overlay': True}
])
page.clean_contents()
for xref in to_remove_xrefs:
imgs = page.get_images(full=True)
for img_data in imgs:
if img_data[0] == xref:
remove_images(doc, page, [img_data[7]])
page.clean_contents()
for insert in to_insert:
img1 = insert[0]
img2 = insert[1]
page.insert_image(img1['bbox'], stream=img1['stream'],
mask=img1['mask'], overlay=img1['overlay'], alpha=0)
page.insert_image(img2['bbox'], stream=img2['stream'],
mask=img2['mask'], overlay=img2['overlay'], alpha=0)
#page.clean_contents()
page.clean_contents()
log = logging.getLogger(__name__)
DEBUG = True
@hookimpl
def add_options(parser):
pass
@hookimpl
def check_options(options):
pass
@hookimpl
def optimize_pdf(
input_pdf: Path,
output_pdf: Path,
context: PdfContext,
executor: Executor,
linearize: bool,
) -> Tuple[Path, Sequence[str]]:
save_settings = dict(
linearize=linearize,
**get_pdf_save_settings(context.options.output_type),
)
result_path = optimizeR(input_pdf, output_pdf, context, save_settings, executor)
messages = []
if context.options.optimize == 0:
messages.append("Optimization was disabled.")
else:
image_optimizers = {
'jbig2': jbig2enc.available(),
'pngquant': pngquant.available(),
}
for name, available in image_optimizers.items():
if not available:
messages.append(
f"The optional dependency '{name}' was not found, so some image "
f"optimizations could not be attempted."
)
return result_path, messages
@hookimpl
def is_optimization_enabled(context: PdfContext) -> bool:
return True
# © 2018 James R. Barlow: github.com/jbarlow83
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""Post-processing image optimization of OCR PDFs."""
log = logging.getLogger(__name__)
DEFAULT_EXECUTOR = SerialExecutor()
def optimizeR(
input_file: Path,
output_file: Path,
context,
save_settings,
executor: Executor = DEFAULT_EXECUTOR,
) -> Path:
options = context.options
log.info(
"In OptimizeR aanbeland"
)
tmpd = tempfile.mkdtemp()
pdfmeta = os.path.join(tmpd, 'pdfmeta.json')
pdfhocr = os.path.join(tmpd, 'pdfhocr.html')
if DEBUG:
stde = sys.stderr
else:
stde = open(os.devnull, 'wb')
out = check_output(['pdf-metadata-json', input_file], stderr=stde)
with open(pdfmeta, 'wb+') as fd:
fd.write(out)
out = check_output(['pdf-to-hocr', '-f', input_file, '-J', pdfmeta], stderr=stde)
with open(pdfhocr, 'wb+') as fd:
fd.write(out)
hocr_iter = hocr_page_iterator(pdfhocr)
doc = fitz.open(input_file)
outfile = output_file
for page in doc:
hocr_page = next(hocr_iter)
hocr_word_data = hocr_page_to_word_data(hocr_page)
compress_page_images(doc, page, hocr_word_data=hocr_word_data)
page.clean_contents()
doc.save(outfile, deflate=True, pretty=True, garbage=2)
oldsize = os.path.getsize(input_file)
newsize = os.path.getsize(output_file)
compression_ratio = oldsize / newsize
print('Compression factor:', compression_ratio, file=sys.stderr)
input_size = input_file.stat().st_size
output_size = output_file.stat().st_size
if output_size == 0:
raise OutputFileAccessError(
f"Output file not created after optimizing. We probably ran "
f"out of disk space in the temporary folder: {tempfile.gettempdir()}."
)
savings = 1 - output_size / input_size
if savings < 0:
log.info(
"Image optimization did not improve the file - "
"optimizations will not be used"
)
return input_file
else:
return output_file
class OptionsStruct(NamedTuple):
subsample: int
slices: None
crcb: int
subsample3 = OptionsStruct(3,[100],djvu.CRCB.full)
subsample12 = OptionsStruct(12,[100],djvu.CRCB.full)
# TODO: Reduce amount of memory active at one given point (keep less images in
# memory, write to disk sooner, etc), careful with numpy <-> PIL conversions
def create_mrc_hocr_components(image, hocr_word_data,
dpi=None,
downsample=None,
bg_downsample=None,
fg_downsample=None,
denoise_mask=None, timing_data=None,
errors=None):
"""
Create the MRC components: mask, foreground and background
Args:
* image (PIL.Image): Image to be decomposed
* hocr_word_data: OCR data about found text on the page
* downsample (int): factor by which the OCR data is to be downsampled
* bg_downsample (int): if the background image should be downscaled
* denoise_mask (bool): Whether to denoise the image if it is deemed too
noisy
* timing_data: Optional timing data to log individual timing data to.
* errors: Optional argument (of type set) with encountered runtime errors
Returns a tuple of the components, as numpy arrays: (mask, foreground,
background)
"""
width_, height_ = image.size
gamera_image = gamera._from_pil(image)
mask2 = gamera.methods['djvu'](gamera_image)
mask3 = mask2.to_greyscale()
#mask3 = _image_conversion.to_greyscale(mask2)
mask = gamera.to_pil_1bpp(mask3)
import numpy as np
mask_arr = np.array(mask)
mask_inv = np.invert(mask_arr)
yield mask_inv
fg_djvu = didjvu.make_layer(gamera_image, mask3, didjvu.subsample_fg, subsample12)
fg_ppm = djvu_to_ppm(fg_djvu)
foreground_arr = np.array(Image.open(fg_ppm))
yield foreground_arr
foreground_arr = None
bg_djvu = didjvu.make_layer(gamera_image, mask3, didjvu.subsample_bg, subsample3)
bg_ppm = djvu_to_ppm(bg_djvu)
background_arr = np.array(Image.open(bg_ppm))
yield background_arr
return
def djvu_to_ppm(djvu_file):
# TODO: Use Multichunk.
ppm_file = temporary.file(suffix='.ppm')
args = ['ddjvu','-format=ppm', djvu_file.name, ppm_file.name]
with open(os.devnull, 'wb') as dev_null:
return utils.Proxy(ppm_file, ipc.Subprocess(args, stderr=dev_null).wait, [djvu_file])
def main(infile, outfile):
from shutil import copy # pylint: disable=import-outside-toplevel
from tempfile import TemporaryDirectory # pylint: disable=import-outside-toplevel
infile = Path(infile)
with TemporaryDirectory() as tmpdir:
context = PdfContext(None, tmpdir, infile, None, None)
tmpout = Path(tmpdir) / 'out.pdf'
optimizeR(
infile,
tmpout,
context,
dict(
compress_streams=True,
preserve_pdfa=True,
object_stream_mode=ObjectStreamMode.generate,
),
)
copy(fspath(tmpout), fspath(outfile))
if __name__ == '__main__':
main(sys.argv[1], sys.argv[2])