50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
# © 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/.
|
|
|
|
|
|
"""Interface to jbig2 executable"""
|
|
|
|
from subprocess import PIPE
|
|
|
|
from ocrmypdf.exceptions import MissingDependencyError
|
|
from ocrmypdf.subprocess import get_version, run
|
|
|
|
|
|
def version():
|
|
return get_version('jbig2', regex=r'jbig2enc (\d+(\.\d+)*).*')
|
|
|
|
|
|
def available():
|
|
try:
|
|
version()
|
|
except MissingDependencyError:
|
|
return False
|
|
return True
|
|
|
|
|
|
def convert_group(*, cwd, infiles, out_prefix):
|
|
args = [
|
|
'jbig2',
|
|
'-b',
|
|
out_prefix,
|
|
'-s', # symbol mode (lossy)
|
|
# '-r', # refinement mode (lossless symbol mode, currently disabled in
|
|
# jbig2)
|
|
'-p',
|
|
]
|
|
args.extend(infiles)
|
|
proc = run(args, cwd=cwd, stdout=PIPE, stderr=PIPE)
|
|
proc.check_returncode()
|
|
return proc
|
|
|
|
|
|
def convert_single(*, cwd, infile, outfile):
|
|
args = ['jbig2', '-p', infile]
|
|
with open(outfile, 'wb') as fstdout:
|
|
proc = run(args, cwd=cwd, stdout=fstdout, stderr=PIPE)
|
|
proc.check_returncode()
|
|
return proc
|