Added support for changing color conversion strategy

This commit is contained in:
James R. Barlow
2023-09-20 01:08:15 -07:00
parent 35a1eaf62a
commit e3c813fc67
11 changed files with 123 additions and 50 deletions
+15 -3
View File
@@ -28,6 +28,17 @@ except AttributeError:
# Pillow 9 shim
Transpose = Image # type: ignore
COLOR_CONVERSION_STRATEGIES = frozenset(
[
'CMYK',
'Gray',
'LeaveColorUnchanged',
'RGB',
'UseDeviceIndependentColor',
]
)
log = logging.getLogger(__name__)
# Most reliable way to get the bitness of Python interpreter, according to Python docs
@@ -160,6 +171,7 @@ def generate_pdfa(
output_file: os.PathLike,
*,
compression: str,
color_conversion_strategy: str,
pdf_version: str = '1.5',
pdfa_part: str = '2',
progressbar_class=None,
@@ -209,16 +221,16 @@ def generate_pdfa(
"-dBATCH",
"-dNOPAUSE",
"-dSAFER",
"-dCompatibilityLevel=" + str(pdf_version),
f"-dCompatibilityLevel={str(pdf_version)}",
"-sDEVICE=pdfwrite",
"-dAutoRotatePages=/None",
"-sColorConversionStrategy=" + strategy,
f"-sColorConversionStrategy={color_conversion_strategy}",
]
+ (['-dPDFSTOPONERROR'] if stop_on_error else [])
+ compression_args
+ [
"-dJPEGQ=95",
"-dPDFA=" + pdfa_part,
f"-dPDFA={pdfa_part}",
"-dPDFACompatibilityPolicy=1",
"-o",
"-",
+1
View File
@@ -793,6 +793,7 @@ def convert_to_pdfa(input_pdf: Path, input_ps_stub: Path, context: PdfContext) -
pdfmark=input_ps_stub,
output_file=output_file,
compression=options.pdfa_image_compression,
color_conversion_strategy=options.color_conversion_strategy,
pdfa_part=options.output_type[-1], # is pdfa-1, pdfa-2, or pdfa-3
progressbar_class=(
context.plugin_manager.hook.get_progressbar_class()
@@ -14,6 +14,20 @@ from ocrmypdf.subprocess import check_external_program
log = logging.getLogger(__name__)
@hookimpl
def add_options(parser):
gs = parser.add_argument_group("Ghostscript", "Advanced control of Ghostscript")
gs.add_argument(
'--color-conversion-strategy',
action='store',
type=str,
metavar='STRATEGY',
choices=ghostscript.COLOR_CONVERSION_STRATEGIES,
default='LeaveColorUnchanged',
help="Set Ghostscript color conversion strategy",
)
@hookimpl
def check_options(options):
"""Check that the options are valid for this plugin."""
@@ -33,6 +47,10 @@ def check_options(options):
if options.output_type == 'pdfa':
options.output_type = 'pdfa-2'
if options.color_conversion_strategy not in ghostscript.COLOR_CONVERSION_STRATEGIES:
raise ValueError(
f"Invalid color conversion strategy: {options.color_conversion_strategy}"
)
@hookimpl
@@ -68,6 +86,7 @@ def generate_pdfa(
pdfmark,
output_file,
compression,
color_conversion_strategy,
pdf_version,
pdfa_part,
progressbar_class,
@@ -78,6 +97,7 @@ def generate_pdfa(
pdf_pages=[*pdf_pages, pdfmark],
output_file=output_file,
compression=compression,
color_conversion_strategy=color_conversion_strategy,
pdf_version=pdf_version,
pdfa_part=pdfa_part,
progressbar_class=progressbar_class,
+3
View File
@@ -467,6 +467,7 @@ def generate_pdfa(
pdfmark: Path,
output_file: Path,
compression: str,
color_conversion_strategy: str,
pdf_version: str,
pdfa_part: str,
progressbar_class,
@@ -489,6 +490,8 @@ def generate_pdfa(
possible. For lossless, all images should be converted to FlateEncode
(lossless PNG). If an empty string, the PDF generator should make its
own decisions about how to encode images.
color_conversion_strategy: A valid Ghostscript color conversion strategy
name.
pdf_version: The minimum PDF version that the output file should be.
At its own discretion, the PDF/A generator may raise the version,
but should not lower it.