read.py 702 B

12345678910111213141516171819202122232425
  1. from PIL import Image
  2. from PIL.ExifTags import TAGS, GPSTAGS
  3. def get_exif_data(image_path):
  4. # 打开图片文件
  5. image = Image.open(image_path)
  6. exif_data = image._getexif() # 获取 EXIF 数据
  7. if not exif_data:
  8. return None # 如果没有 EXIF 数据,返回 None
  9. # 转换 EXIF 标签为人类可读格式
  10. readable_exif = {}
  11. for tag_id, value in exif_data.items():
  12. tag_name = TAGS.get(tag_id, tag_id)
  13. readable_exif[tag_name] = value
  14. return readable_exif
  15. # 使用示例
  16. image_path = "2.jpg"
  17. exif = get_exif_data(image_path)
  18. if exif:
  19. for key, value in exif.items():
  20. print(f"{key}: {value}")
  21. else:
  22. print("No EXIF data found.")