From 15972b9c0fefb5b9997818736939786858d20885 Mon Sep 17 00:00:00 2001 From: arkohut <39525455+arkohut@users.noreply.github.com> Date: Wed, 21 Aug 2024 19:16:07 +0800 Subject: [PATCH] feat: use utiles for read metadata --- memos/read_metadata.py | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/memos/read_metadata.py b/memos/read_metadata.py index bb59cfd..6715bb4 100644 --- a/memos/read_metadata.py +++ b/memos/read_metadata.py @@ -1,36 +1,16 @@ -import piexif import json import argparse -from PIL import Image, PngImagePlugin +from .utils import get_image_metadata def read_metadata(image_path): try: - img = Image.open(image_path) - exif_data = img.info.get("exif") - png_info = img.info if isinstance(img, PngImagePlugin.PngImageFile) else None + metadata = get_image_metadata(image_path) - if not exif_data and not png_info: - print("No EXIF or PNG metadata found.") + if metadata is None: + print("No metadata found or unsupported file format.") return None - metadata = {} - - if exif_data: - exif_dict = piexif.load(exif_data) - metadata_json = exif_dict["0th"].get(piexif.ImageIFD.ImageDescription) - if metadata_json: - metadata = json.loads(metadata_json.decode()) - else: - print("No metadata found in the ImageDescription field of EXIF.") - - if png_info: - metadata_json = png_info.get("Description") - if metadata_json: - metadata = json.loads(metadata_json) - else: - print("No metadata found in the Description field of PNG.") - return metadata if metadata else None except Exception as e: @@ -49,4 +29,4 @@ def main(): if __name__ == "__main__": - main() + main() \ No newline at end of file