mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-06 03:05:25 +00:00
feat: use utiles for read metadata
This commit is contained in:
parent
b3e73e4c40
commit
15972b9c0f
@ -1,36 +1,16 @@
|
|||||||
import piexif
|
|
||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
from PIL import Image, PngImagePlugin
|
from .utils import get_image_metadata
|
||||||
|
|
||||||
|
|
||||||
def read_metadata(image_path):
|
def read_metadata(image_path):
|
||||||
try:
|
try:
|
||||||
img = Image.open(image_path)
|
metadata = get_image_metadata(image_path)
|
||||||
exif_data = img.info.get("exif")
|
|
||||||
png_info = img.info if isinstance(img, PngImagePlugin.PngImageFile) else None
|
|
||||||
|
|
||||||
if not exif_data and not png_info:
|
if metadata is None:
|
||||||
print("No EXIF or PNG metadata found.")
|
print("No metadata found or unsupported file format.")
|
||||||
return None
|
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
|
return metadata if metadata else None
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -49,4 +29,4 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
Loading…
x
Reference in New Issue
Block a user