-
Notifications
You must be signed in to change notification settings - Fork 0
/
photorec-rename.py
executable file
·185 lines (140 loc) · 4.41 KB
/
photorec-rename.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
"""
Better organise and rename files recovered by ``photorec``.
Copies only good JPEG photos to another directory, using the EXIF date
the photo was taken as the file name.
See:
https://www.cgsecurity.org/wiki/PhotoRec
"""
import argparse
import datetime
import logging
from pathlib import Path
import shutil
import sys
from typing import Iterator, Optional
from PIL import UnidentifiedImageError
from PIL.Image import Exif, Image, open as open_image
logger = logging.getLogger(__name__)
def argparse_empty_folder(string: str) -> Path:
"""
An `argparse` type to check path is an empty folder.
Raises:
argparse.ArgumentTypeError:
If path is not an existing folder.
Return:
Path to existing folder.
"""
path = argparse_existing_folder(string)
is_empty = not any(path.iterdir())
if not is_empty:
message = f"Folder is not empty: {path}"
raise argparse.ArgumentTypeError(message)
return path
def argparse_existing_folder(string: str) -> Path:
"""
An `argparse` type to convert string to a `Path` object.
Raises:
argparse.ArgumentTypeError:
If path is not an existing folder.
Return:
Path to existing folder.
"""
path = Path(string).expanduser().resolve()
error = None
if not path.exists():
error = f"Folder does not exist: {path}"
if not path.is_dir():
error = f"Path is not a folder: {path}"
if error is not None:
raise argparse.ArgumentTypeError(error)
return path
def build_file_name(image: Image) -> Optional[str]:
exif = read_exif(image)
if not exif:
return None
photo_taken = parse_datetime(exif)
if not photo_taken:
return None
date = photo_taken.strftime("%Y-%m-%d-%H%M%S")
return f"{date}.{image.format.lower()}"
def list_files(root: Path) -> Iterator[Path]:
"""
Recursively list files under root.
"""
for path in root.glob('**/*'):
if path.is_file():
yield path
def read_exif(image: Image) -> Optional[Exif]:
exif = image.getexif()
if not exif:
logger.debug("Could not find Exif metadata in %s", image.filename)
return exif
def read_image(path: Path) -> Optional[Image]:
"""
Attempt to open image file.
Args:
path:
Path to image file.
Return:
An Image object, or None if input wasn't an image.
"""
# Attempt to open as image
try:
image = open_image(path)
except UnidentifiedImageError:
return None
except OSError as e:
logger.warning("%s: %s", e, path)
return None
else:
return image
def parse_datetime(exif: Exif) -> Optional[datetime.datetime]:
"""
Return time photo taken from DateTime string in given exif data.
Use the Exif.Image.DateTime field, number 306.
Args:
string:
Datetime in exif format 'YYYY:MM:DD HH:MM:SS'
Returns:
Datetime, or none if datetime not present.
"""
string = exif.get(306)
if string is None:
return None
parsed = datetime.datetime.strptime(string, "%Y:%m:%d %H:%M:%S")
return parsed
def main(options: argparse.Namespace) -> int:
for path in list_files(options.photorec):
image = read_image(path)
# Ignore non-image files
if not image:
continue
# Ignore small images
if image.height < 1000:
continue
# Calculate new name
file_name = build_file_name(image)
if not file_name:
continue
# Copy to new name
logger.info("Copy %s to %s", path.name, file_name)
shutil.copy(path, options.output / file_name)
return 0
def parse(arguments: list[str]) -> argparse.Namespace:
description = "Better organise and rename files recovered by 'photorec'"
parser = argparse.ArgumentParser(description=description)
parser.add_argument(
metavar='PHOTOREC', dest='photorec', type=argparse_existing_folder,
help="Folder of files recovered by photorec",
)
parser.add_argument(
metavar='OUTPUT', dest='output', type=argparse_empty_folder,
help="Folder to output renamed files",
)
return parser.parse_args()
if __name__ == '__main__':
options = parse(sys.argv[1:])
logging.basicConfig(format="%(message)s", level=logging.INFO)
return_code = main(options)
sys.exit(return_code)