Add support for adjusting jbig2 threshold

This commit is contained in:
James R. Barlow
2023-09-17 14:44:40 -07:00
parent 4bee7355e9
commit ac2fc49208
4 changed files with 28 additions and 7 deletions
+13 -7
View File
@@ -23,15 +23,17 @@ def available():
return True
def convert_group(*, cwd, infiles, out_prefix):
def convert_group(*, cwd, infiles, out_prefix, threshold):
args = [
'jbig2',
'-b',
out_prefix,
'-s', # symbol mode (lossy)
'--symbol-mode', # symbol mode (lossy)
'-t',
str(threshold), # threshold
# '-r', # refinement mode (lossless symbol mode, currently disabled in
# jbig2)
'-p',
'--pdf',
]
args.extend(infiles)
proc = run(args, cwd=cwd, stdout=PIPE, stderr=PIPE)
@@ -40,11 +42,13 @@ def convert_group(*, cwd, infiles, out_prefix):
def convert_group_mp(args):
return convert_group(cwd=args[0], infiles=args[1], out_prefix=args[2])
return convert_group(
cwd=args[0], infiles=args[1], out_prefix=args[2], threshold=args[3]
)
def convert_single(*, cwd, infile, outfile):
args = ['jbig2', '-p', infile]
def convert_single(*, cwd, infile, outfile, threshold):
args = ['jbig2', '--pdf', '-t', str(threshold), infile]
with open(outfile, 'wb') as fstdout:
proc = run(args, cwd=cwd, stdout=fstdout, stderr=PIPE)
proc.check_returncode()
@@ -52,4 +56,6 @@ def convert_single(*, cwd, infile, outfile):
def convert_single_mp(args):
return convert_single(cwd=args[0], infile=args[1], outfile=args[2])
return convert_single(
cwd=args[0], infile=args[1], outfile=args[2], threshold=args[3]
)
+10
View File
@@ -86,6 +86,16 @@ def add_options(parser):
# Adjust number of pages to consider at once for JBIG2 compression
help=argparse.SUPPRESS,
)
optimizing.add_argument(
'--jbig2-threshold',
type=numeric(float, 0.4, 0.9),
default=0.85,
metavar='T',
help=(
"Adjust JBIG2 symbol code classification threshold "
"(default 0.85), range 0.4 to 0.9."
),
)
@hookimpl
+3
View File
@@ -367,6 +367,7 @@ def _produce_jbig2_images(
fspath(root), # =cwd
(img_name(root, xref, ext) for xref, ext in xref_exts), # =infiles
prefix, # =out_prefix
options.jbig2_threshold,
)
def jbig2_single_args(root, groups: dict[int, list[XrefExt]]):
@@ -379,6 +380,7 @@ def _produce_jbig2_images(
fspath(root),
img_name(root, xref, ext),
root / f'{prefix}.{n:04d}',
options.jbig2_threshold,
)
if options.jbig2_page_group_size > 1:
@@ -737,6 +739,7 @@ def main(infile, outfile, level, jobs=1):
self.png_quality = png_quality
self.jbig2_page_group_size = 0
self.jbig2_lossy = jb2lossy
self.jbig2_threshold = 0.85
self.quiet = True
self.progress_bar = False