-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_meta_extractor.py
More file actions
59 lines (48 loc) · 1.79 KB
/
image_meta_extractor.py
File metadata and controls
59 lines (48 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
def get_image_metadata(image_path):
# Open the image file
image = Image.open(image_path)
# Extract the EXIF metadata
exif_data = image._getexif()
if exif_data is not None:
# Create a dictionary to store the metadata
metadata = {}
for tag_id, value in exif_data.items():
tag_name = TAGS.get(tag_id, tag_id)
# Extract DateTimeOriginal tag
if tag_name == 'DateTimeOriginal':
metadata[tag_name] = value
# Extract GPSInfo tag
if tag_name == 'GPSInfo':
gps_data = {}
for gps_tag_id in value:
gps_tag_name = GPSTAGS.get(gps_tag_id, gps_tag_id)
gps_data[gps_tag_name] = value[gps_tag_id]
metadata[tag_name] = gps_data
return metadata
else:
return None
# Provide the path to your image file
image_path = "testImg.jpg"
# Call the function to extract metadata
metadata = get_image_metadata(image_path)
# Print the extracted metadata
if metadata is not None:
if 'DateTimeOriginal' in metadata:
print(f"DateTimeOriginal: {metadata['DateTimeOriginal']}")
else:
print("DateTimeOriginal tag not found.")
if 'GPSInfo' in metadata:
gps_data = metadata['GPSInfo']
if 'GPSLatitude' in gps_data and 'GPSLongitude' in gps_data:
latitude = gps_data['GPSLatitude']
longitude = gps_data['GPSLongitude']
print(f"GPS Latitude: {latitude}")
print(f"GPS Longitude: {longitude}")
else:
print("GPS coordinates not found.")
else:
print("GPSInfo tag not found.")
else:
print("No metadata found.")