-
Notifications
You must be signed in to change notification settings - Fork 1
/
dngconverter.py
69 lines (51 loc) · 1.92 KB
/
dngconverter.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
import argparse
from concurrent.futures import ProcessPoolExecutor, as_completed
from pathlib import Path
from pidng.core import RPICAM2DNG
from tqdm import tqdm
def bayerjpg2dng(jpg_path: Path, delete: bool = False) -> None:
"""
Convert a the file at `jpg_path` to a `.dng` file. Deletes the old file if `delete`
is set to `True`.
"""
d = RPICAM2DNG()
d.convert(str(jpg_path))
dng_path = jpg_path.with_suffix(".dng")
validate_file(dng_path, min_bytes=int(1e6))
if delete:
jpg_path.unlink()
def validate_file(path: Path, min_bytes: int = 1) -> None:
"""Check if file exists and has a file size larger than `min_bytes`."""
is_valid = path.is_file() and path.stat().st_size > min_bytes
if not is_valid:
raise InvalidFileException(path)
class InvalidFileException(Exception):
"""Exception raised when a file was found to be invalid."""
def __init__(self, filepath: Path) -> None:
super().__init__(f'File "{filepath}" is invalid.')
def parse_arguments() -> argparse.Namespace:
"""
Parse command line arguments to the Pi to DNG conversion utility. Returns the `args`
object.
"""
parser = argparse.ArgumentParser(
description="Convert Raspberry Pi Bayer JPEGs to DNGs."
)
parser.add_argument("directory", help="Directory with the JPEG files")
parser.add_argument("--delete", action="store_true")
return parser.parse_args()
def main() -> None:
args = parse_arguments()
directory = Path(args.directory)
filepaths = list(directory.glob("frame-*.jpg"))
with tqdm(total=len(filepaths)) as pbar:
executor = ProcessPoolExecutor(max_workers=2)
futures = [
executor.submit(bayerjpg2dng, path, delete=args.delete)
for path in filepaths
]
for future in as_completed(futures):
_ = future.result()
pbar.update()
if __name__ == "__main__":
main()