dotfiles/config/scripts/picture_name_date

51 lines
1.5 KiB
Plaintext
Raw Normal View History

2019-08-04 19:05:57 +02:00
#!/usr/bin/env python
import os
import re
import typing
import PIL.ExifTags
import PIL.Image
import progressbar
EXTENSION_PATTERN = re.compile(r'\.JPE?G', re.I)
COMMON_PATTERN = re.compile(r'(IMG|DSC[NF]?|100|P10)_?\d+', re.I)
EXIF_TAG_NAME = 'DateTimeOriginal'
EXIF_TAG_ID = list(PIL.ExifTags.TAGS.keys())[list(
PIL.ExifTags.TAGS.values()).index(EXIF_TAG_NAME)]
def get_pictures(directory: str = ".", skip_renamed: bool = True) \
-> typing.Generator:
for root, _, files in os.walk(directory):
for filename in files:
filename_trunk, extension = os.path.splitext(filename)
# if extension.upper() not in ('.JPEG', '.JPG'):
if not re.match(EXTENSION_PATTERN, extension):
continue
if skip_renamed:
if not re.match(COMMON_PATTERN, filename_trunk):
continue
full_path = os.path.join(root, filename)
yield full_path
def main() -> None:
print("Counting files...")
nb_imgs = len(list(get_pictures()))
print("Processing files...")
iterator = progressbar.progressbar(get_pictures(), max_value=nb_imgs)
for full_path in iterator:
img = PIL.Image.open(full_path)
exif_data = img._getexif()
if exif_data and EXIF_TAG_ID in exif_data:
date_raw = exif_data[EXIF_TAG_ID]
# print(date_raw, full_path)
img.close()
if __name__ == "__main__":
# TODO Arguments parsing
main()