In this lesson, we are going to learn how to use Python to extract image file metadata.
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Source Code:
import os
from PIL import Image
from PIL.ExifTags import TAGS
import pandas as pd
img_file = 'DSCF4025.jpg'
image = Image.open(img_file)
for tag, value in image._getexif().items():
if tag in TAGS:
print(TAGS[tag])
exif[TAGS[tag]] = value
# list comprehension
exif = {TAGS[k]: v for k, v in image._getexif().items() if k in TAGS}
if 'GPSInfo' in exif:
# to convert to Geodetic Coordinate
geo_coodinate = '{0} {1} {2:.2f} {3}, {4} {5} {6:.2f} {7}'.format(
exif['GPSInfo'][2][0][0],
exif['GPSInfo'][2][1][0],
exif['GPSInfo'][2][2][0] / exif['GPSInfo'][2][2][1],
exif['GPSInfo'][1],
exif['GPSInfo'][4][0][0],
exif['GPSInfo'][4][1][0],
exif['GPSInfo'][4][2][0] / exif['GPSInfo'][2][2][1],
exif['GPSInfo'][3]
)
print(geo_coodinate)