|
| 1 | +import os |
| 2 | +import exifread |
| 3 | +import argparse |
| 4 | +import dateutil.parser as du |
| 5 | + |
| 6 | +def main(): |
| 7 | + parser = argparse.ArgumentParser(description="Find images by their EXIF data") |
| 8 | + parser.add_argument("dir", type=str, help="base dir to search in") |
| 9 | + parser.add_argument("--before", type=str, help="date to be digitized before in format YY-MM-DD") |
| 10 | + parser.add_argument("--after", type=str, help="date to be digitized after in format YY-MM-DD") |
| 11 | + parser.add_argument("--ext", nargs=1, type=str, help="search only for files with $EXT extensions") |
| 12 | + parser.add_argument("--orientation", type=str, choices=["Horizontal", "Vertical"]) |
| 13 | + parser.add_argument("--author", type=str) |
| 14 | + parser.add_argument("--software", type=str) |
| 15 | + |
| 16 | + args = dict(parser.parse_args().__dict__) |
| 17 | + check(args) |
| 18 | + |
| 19 | + |
| 20 | +def check(args: dict): |
| 21 | + for dir, file in enumerate_files(args.get("dir", "."), args.get("ext", None)): |
| 22 | + with open(os.path.join(dir, file), "rb") as f: |
| 23 | + tags = exifread.process_file(f, details=False) |
| 24 | + if len(tags) is 0: |
| 25 | + continue |
| 26 | + |
| 27 | + ok = True |
| 28 | + ok &= check_daterange( tags, args.get("before", None), args.get("after", None)) |
| 29 | + ok &= check_orientation(tags, args.get("orientation", None)) |
| 30 | + ok &= check_author( tags, args.get("author", None)) |
| 31 | + ok &= check_software( tags, args.get("software", None)) |
| 32 | + if ok is True: |
| 33 | + print(os.path.join(dir, file)) |
| 34 | + |
| 35 | + |
| 36 | +def check_ext(file: str, exts: list = None): |
| 37 | + if exts is None: |
| 38 | + return True |
| 39 | + for ext in exts: |
| 40 | + if ext: |
| 41 | + _ext = file[-len(ext):].lower() |
| 42 | + if _ext == ext.lower(): |
| 43 | + return True |
| 44 | + return False |
| 45 | + |
| 46 | + |
| 47 | +def check_daterange(tags, before:str=None, after:str=None): |
| 48 | + def _check(dat): |
| 49 | + if before is not None: |
| 50 | + if dat >= before: |
| 51 | + return False |
| 52 | + if after is not None: |
| 53 | + if dat <= after: |
| 54 | + return False |
| 55 | + return True |
| 56 | + |
| 57 | + if before is None and after is None: |
| 58 | + return True |
| 59 | + |
| 60 | + before = du.parse(before) if before is not None else before |
| 61 | + after = du.parse(after) if after is not None else after |
| 62 | + |
| 63 | + date_args = ("EXIF DateTimeDigitized", "EXIF DateTimeOriginal", "Image DateTime") |
| 64 | + for arg in date_args: |
| 65 | + if arg in tags: |
| 66 | + value = str(tags[arg]) |
| 67 | + if not value: |
| 68 | + continue |
| 69 | + if value.count(":") > 3: |
| 70 | + value = value.replace(":", "-", 2) |
| 71 | + value = du.parse(value) |
| 72 | + if value and _check(value): |
| 73 | + return True |
| 74 | + return False |
| 75 | + |
| 76 | + |
| 77 | +def check_tag(tags: dict, key: str, value: str) -> bool: |
| 78 | + if value is None: |
| 79 | + return True |
| 80 | + if key in tags: |
| 81 | + _value = str(tags[key]).lower() |
| 82 | + if _value.find(value.lower()) > -1: |
| 83 | + return True |
| 84 | + return False |
| 85 | + |
| 86 | + |
| 87 | +def check_orientation(tags, orientation: str = None): |
| 88 | + return check_tag(tags, "Image Orientation", orientation) |
| 89 | + |
| 90 | + |
| 91 | +def check_author(tags, author: str = None): |
| 92 | + author_args = ("Image Artist", "Image XPAuthor") |
| 93 | + for arg in author_args: |
| 94 | + if check_tag(tags, arg, author): |
| 95 | + return True |
| 96 | + return False |
| 97 | + |
| 98 | + |
| 99 | +def check_software(tags, software: str = None): |
| 100 | + return check_tag(tags, "Image Software", software) |
| 101 | + |
| 102 | + |
| 103 | +def enumerate_files(dir: str, exts: list = None): |
| 104 | + for root, _, files in os.walk(dir): |
| 105 | + for file in files: |
| 106 | + if check_ext(file, exts): |
| 107 | + yield root, file |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + main() |
0 commit comments