Added --export-edited to export
This commit is contained in:
@@ -3,6 +3,7 @@ import datetime
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
|
import pathlib
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import click
|
import click
|
||||||
@@ -10,9 +11,8 @@ import yaml
|
|||||||
|
|
||||||
import osxphotos
|
import osxphotos
|
||||||
|
|
||||||
from ._version import __version__
|
|
||||||
from ._constants import _EXIF_TOOL_URL
|
from ._constants import _EXIF_TOOL_URL
|
||||||
|
from ._version import __version__
|
||||||
|
|
||||||
# TODO: add "--any" to search any field (e.g. keyword, description, title contains "wedding") (add case insensitive option)
|
# TODO: add "--any" to search any field (e.g. keyword, description, title contains "wedding") (add case insensitive option)
|
||||||
|
|
||||||
@@ -353,19 +353,26 @@ def query(
|
|||||||
)
|
)
|
||||||
@click.option("--hidden", is_flag=True, help="Search for photos marked hidden.")
|
@click.option("--hidden", is_flag=True, help="Search for photos marked hidden.")
|
||||||
@click.option("--not-hidden", is_flag=True, help="Search for photos not marked hidden.")
|
@click.option("--not-hidden", is_flag=True, help="Search for photos not marked hidden.")
|
||||||
@click.option("--verbose", is_flag=True, help="Print verbose output")
|
@click.option("--verbose", is_flag=True, help="Print verbose output.")
|
||||||
@click.option(
|
@click.option(
|
||||||
"--overwrite",
|
"--overwrite",
|
||||||
is_flag=True,
|
is_flag=True,
|
||||||
help="Overwrite existing files. "
|
help="Overwrite existing files. "
|
||||||
"Default behavior is to add (1), (2), etc to filename if file already exists. "
|
"Default behavior is to add (1), (2), etc to filename if file already exists. "
|
||||||
"Use this with caution as it may create name collisions on export "
|
"Use this with caution as it may create name collisions on export. "
|
||||||
"(e.g. if two files happen to have the same name)",
|
"(e.g. if two files happen to have the same name)",
|
||||||
)
|
)
|
||||||
@click.option(
|
@click.option(
|
||||||
"--export-by-date",
|
"--export-by-date",
|
||||||
is_flag=True,
|
is_flag=True,
|
||||||
help="Automatically create output folders to organize photos by date created (e.g. DEST/2019/12/20/photoname.jpg)",
|
help="Automatically create output folders to organize photos by date created "
|
||||||
|
"(e.g. DEST/2019/12/20/photoname.jpg).",
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--export-edited",
|
||||||
|
is_flag=True,
|
||||||
|
help="Also export edited version of photo "
|
||||||
|
'if an edited version exists. Edited photo will be named in form of "photoname_edited.ext"',
|
||||||
)
|
)
|
||||||
@click.option(
|
@click.option(
|
||||||
"--sidecar",
|
"--sidecar",
|
||||||
@@ -400,6 +407,7 @@ def export(
|
|||||||
verbose,
|
verbose,
|
||||||
overwrite,
|
overwrite,
|
||||||
export_by_date,
|
export_by_date,
|
||||||
|
export_edited,
|
||||||
sidecar,
|
sidecar,
|
||||||
dest,
|
dest,
|
||||||
):
|
):
|
||||||
@@ -412,8 +420,6 @@ def export(
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO: --export-edited, --export-original
|
# TODO: --export-edited, --export-original
|
||||||
# todo: add sidecar
|
|
||||||
# TODO: add tqdm
|
|
||||||
|
|
||||||
if not os.path.isdir(dest):
|
if not os.path.isdir(dest):
|
||||||
sys.exit("DEST must be valid path")
|
sys.exit("DEST must be valid path")
|
||||||
@@ -449,11 +455,19 @@ def export(
|
|||||||
# show progress bar
|
# show progress bar
|
||||||
with click.progressbar(photos) as bar:
|
with click.progressbar(photos) as bar:
|
||||||
for p in bar:
|
for p in bar:
|
||||||
export_photo(p, dest, verbose, export_by_date, sidecar, overwrite)
|
export_photo(
|
||||||
|
p,
|
||||||
|
dest,
|
||||||
|
verbose,
|
||||||
|
export_by_date,
|
||||||
|
sidecar,
|
||||||
|
overwrite,
|
||||||
|
export_edited,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
for p in photos:
|
for p in photos:
|
||||||
export_path = export_photo(
|
export_path = export_photo(
|
||||||
p, dest, verbose, export_by_date, sidecar, overwrite
|
p, dest, verbose, export_by_date, sidecar, overwrite, export_edited
|
||||||
)
|
)
|
||||||
click.echo(f"Exported {p.filename} to {export_path}")
|
click.echo(f"Exported {p.filename} to {export_path}")
|
||||||
else:
|
else:
|
||||||
@@ -616,7 +630,9 @@ def _query(
|
|||||||
return photos
|
return photos
|
||||||
|
|
||||||
|
|
||||||
def export_photo(photo, dest, verbose, export_by_date, sidecar, overwrite):
|
def export_photo(
|
||||||
|
photo, dest, verbose, export_by_date, sidecar, overwrite, export_edited
|
||||||
|
):
|
||||||
""" Helper function for export that does the actual export
|
""" Helper function for export that does the actual export
|
||||||
photo: PhotoInfo object
|
photo: PhotoInfo object
|
||||||
dest: destination path as string
|
dest: destination path as string
|
||||||
@@ -636,7 +652,20 @@ def export_photo(photo, dest, verbose, export_by_date, sidecar, overwrite):
|
|||||||
if export_by_date:
|
if export_by_date:
|
||||||
date_created = photo.date.timetuple()
|
date_created = photo.date.timetuple()
|
||||||
dest = create_path_by_date(dest, date_created)
|
dest = create_path_by_date(dest, date_created)
|
||||||
return photo.export(dest, sidecar=sidecar, overwrite=overwrite)
|
photo_path = photo.export(dest, sidecar=sidecar, overwrite=overwrite)
|
||||||
|
|
||||||
|
# if export-edited, also export the edited version
|
||||||
|
# verify the photo has adjustments and valid path to avoid raising an exception
|
||||||
|
if export_edited and photo.hasadjustments and photo.path_edited is not None:
|
||||||
|
edited_name = pathlib.Path(photo.filename)
|
||||||
|
edited_name = f"{edited_name.stem}_edited{edited_name.suffix}"
|
||||||
|
if verbose:
|
||||||
|
click.echo(f"Exporting edited version of {photo.filename} as {edited_name}")
|
||||||
|
photo.export(
|
||||||
|
dest, edited_name, sidecar=sidecar, overwrite=overwrite, edited=True
|
||||||
|
)
|
||||||
|
|
||||||
|
return photo_path
|
||||||
|
|
||||||
|
|
||||||
def create_path_by_date(dest, dt):
|
def create_path_by_date(dest, dt):
|
||||||
|
|||||||
Reference in New Issue
Block a user