-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmarkphotos.py
110 lines (98 loc) · 3.03 KB
/
markphotos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /usr/bin/env python
# file: markphotos.py
# vim:fileencoding=utf-8:ft=python
#
# Copyright © 2011-2018 R.F. Smith <[email protected]>.
# SPDX-License-Identifier: MIT
# Created: 2011-11-06T20:28:07+01:00
# Last modified: 2020-04-01T20:06:20+0200
"""Script to add my copyright notice to photos."""
from os import utime
from time import mktime
import argparse
import concurrent.futures as cf
import logging
import os.path
import subprocess as sp
import sys
__version__ = "2020.04.01"
def main():
"""
Entry point for markphotos.
"""
args = setup()
with cf.ThreadPoolExecutor(max_workers=os.cpu_count()) as tp:
for fn, rv in tp.map(processfile, args.files):
logging.info(f'file "{fn}" processed.')
if rv != 0:
logging.error(f'error processing "{fn}": {rv}')
def setup():
"""Process command-line arguments. Check for required program."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--log",
default="warning",
choices=["debug", "info", "warning", "error"],
help="logging level (defaults to 'warning')",
)
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument(
"files", metavar="file", nargs="+", help="one or more files to process"
)
args = parser.parse_args(sys.argv[1:])
logging.basicConfig(
level=getattr(logging, args.log.upper(), None),
format="%(levelname)s: %(message)s",
)
logging.debug(f"command line arguments = {sys.argv}")
logging.debug(f"parsed arguments = {args}")
# Check for required programs.
try:
sp.run(["exiftool"], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.debug("found “exiftool”")
except FileNotFoundError:
logging.error("the “exiftool” program cannot be found")
sys.exit(1)
return args
def processfile(name):
"""
Add copyright notice to a file using exiftool.
Arguments:
name: path of the file to change
Returns:
A 2-tuple of the file path and the return value of exiftool.
"""
args = ["exiftool", "-CreateDate", name]
cp = sp.run(args, stdout=sp.PIPE, stderr=sp.DEVNULL, text=True)
fields = cp.stdout.split(":")
year = int(fields[1])
cr = "R.F. Smith <[email protected]> http://rsmith.home.xs4all.nl/"
cmt = f"Copyright © {year} {cr}"
args = [
"exiftool",
f'-Copyright="Copyright (C) {year} {cr}"',
f'-Comment="{cmt}"',
"-overwrite_original",
"-q",
name,
]
cp = sp.run(args, stdout=sp.DEVNULL, stderr=sp.DEVNULL)
modtime = int(
mktime(
(
year,
int(fields[2]),
int(fields[3][:2]),
int(fields[3][3:]),
int(fields[4]),
int(fields[5]),
0,
0,
-1,
)
)
)
utime(name, (modtime, modtime))
return name, cp.returncode
if __name__ == "__main__":
main()