Created
May 3, 2020 14:56
Copy values between RawTherapee pp3 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Command line tool for copying values between RawTherapee pp3 files. | |
It was mainly used to copy crop values between raw and jpeg files, but can be adapted for any other need. | |
Processing multiple files in bash: | |
for f in *CR2.pp3; do python rawtherapee.py -f $f `basename $f .CR2.pp3`.JPG.pp3; done | |
""" | |
from configparser import ConfigParser | |
from pathlib import Path | |
import click | |
@click.command(help='Copy crop, rotation and size info from src to dst pp3 file.') | |
@click.argument('src') | |
@click.argument('dst') | |
@click.option('-t', '--template', | |
default="", | |
help='File to use as template if dst file does not exist.') | |
@click.option('-f', '--force', is_flag=True, help='Overwrite existing files.') | |
def clone_geometry(src, dst, template, force): | |
src_config = _get_parser(src) | |
dst_file = Path(dst) | |
dst_config = _get_parser(dst) if dst_file.exists() else _get_parser(template) | |
sections_to_copy = ['Version', 'Crop', 'Coarse Transformation', 'Rotation', 'Resize'] | |
try: | |
for name in sections_to_copy: | |
dst_config[name] = src_config[name] | |
except KeyError: | |
raise click.ClickException(f'Section "{name}" not found in src file.') | |
_write_config(dst_config, dst_file, force) | |
def _get_parser(path: str): | |
"""Create properly configured parser and load config file if given.""" | |
config = ConfigParser() | |
config.optionxform = lambda x: x # preserve letter case | |
if path: | |
config.read(path) | |
return config | |
def _write_config(config, file, force): | |
"""Write configuration to file while obeying the force flag.""" | |
if file.exists() and not force: | |
raise click.ClickException('File already exists.') | |
else: | |
with file.open('w') as f: | |
config.write(f, space_around_delimiters=False) | |
if __name__ == '__main__': | |
clone_geometry() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment