Last active
October 31, 2021 10:50
-
-
Save NicHub/a6767d4428fac967ba7bd60ed9719311 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
from PIL import Image | |
def get_file_list(): | |
""" ___ """ | |
path = os.getcwd() + os.sep | |
print("\nSEARCH IN : " + path + "\n") | |
file_list = [] | |
for root, _, files in os.walk(path, topdown=True): | |
for file in sorted(files): | |
_, ext = os.path.splitext(file) | |
if ext.upper() in [".JPG", ".JPEG", ".PNG"]: | |
file_list.append(os.path.join(root, file)) | |
return file_list | |
def get_picture_metadata(file_list): | |
""" ___ """ | |
metadatas = [] | |
for file in file_list: | |
with Image.open(file) as im1: | |
metadata = {} | |
metadata["width"] = im1.width | |
metadata["height"] = im1.height | |
metadata["format"] = im1.format | |
metadata["mode"] = im1.mode | |
metadata["palette"] = "palette None" if im1.palette is None else "palette Yes" | |
metadata["full_path"] = im1.filename | |
metadata["filename"] = os.path.split(im1.filename)[-1] | |
metadata["date_time"] = "" | |
if (img_exif := im1.getexif()) is not None: | |
# To read all exif data see | |
# https://stackoverflow.com/a/56571871/3057377 | |
if 306 in img_exif: | |
metadata["date_time"] = f"{img_exif[306]}" | |
metadatas.append(metadata) | |
display_metadata([metadata]) | |
return metadatas | |
def display_metadata(metadatas): | |
""" ___ """ | |
for metadata in metadatas: | |
print( | |
f'{metadata["width"]:5} × {metadata["height"]:5} — ' | |
f'format {metadata["format"]:5} — ' | |
f'mode {metadata["mode"]:4} — ' | |
f'{metadata["palette"]:12} — ' | |
f'{metadata["full_path"]} — ' | |
f'{metadata["filename"]} — ' | |
f'{metadata["date_time"]}' | |
) | |
if __name__ == "__main__": | |
file_list = get_file_list() | |
metadatas = get_picture_metadata(file_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment