-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexiftool_scraper.py
More file actions
174 lines (148 loc) · 6.06 KB
/
exiftool_scraper.py
File metadata and controls
174 lines (148 loc) · 6.06 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
exiftool_scraper.py
Scrapes image metadata using the external ExifTool binary (preferred)
and falls back to Pillow for basic info if ExifTool is unavailable.
"""
import subprocess
import json
from pathlib import Path
from PIL import Image
from PIL.ExifTags import TAGS
import os
from datetime import datetime
class MetadataScraper:
def __init__(self, exiftool_path: str = "exiftool"):
"""
Initialize the MetadataScraper.
Args:
exiftool_path (str): Path to the ExifTool executable.
"""
self.exiftool_path = exiftool_path
def scrape(self, file_path: str) -> dict:
"""
Scrape metadata from the given file path.
This will first try ExifTool (with JSON output). If that fails,
it will fall back to PIL’s internal EXIF parser for core tags.
Args:
file_path (str): Path to the image file to analyze.
Returns:
dict: Dictionary containing all scraped metadata.
"""
file = Path(file_path)
if not file.exists():
return {"Error": f"File not found: {file_path}"}
metadata = {}
# --- 1) Try ExifTool JSON ---
try:
# "-j" => JSON output, "-n" => numeric values where appropriate
result = subprocess.run(
[self.exiftool_path, "-j", "-n", str(file)],
capture_output=True,
check=True,
text=True,
)
# ExifTool returns a JSON array; we take the first element
data = json.loads(result.stdout)
if isinstance(data, list) and data:
metadata.update(data[0])
else:
metadata["Warning"] = "ExifTool returned no data"
except (subprocess.CalledProcessError, FileNotFoundError, json.JSONDecodeError) as e:
# Fallback to PIL if ExifTool not found or errors out
metadata["ExifTool Error"] = str(e)
metadata.update(self._pillow_fallback(file_path))
return metadata
def _pillow_fallback(self, file_path: str) -> dict:
"""
Fallback metadata extraction via Pillow.
Args:
file_path (str): Path to the image file.
Returns:
dict: Basic metadata from Pillow.
"""
info = {}
try:
img = Image.open(file_path)
info = {
"Filename": img.filename,
"Image Size": f"{img.width}x{img.height}",
"Image Format": img.format,
"Image Mode": img.mode,
}
exif_data = img._getexif()
if exif_data:
for tag_id, value in exif_data.items():
tag = TAGS.get(tag_id, tag_id)
# decode bytes if necessary
if isinstance(value, bytes):
try:
value = value.decode(errors="ignore")
except Exception:
pass
info[tag] = value
except Exception as e:
info = {"Pillow Error": f"Failed to read image: {e}"}
return info
def display_metadata(self, metadata: dict):
"""
Print metadata in a human-readable table format.
Args:
metadata (dict): Dictionary containing metadata to display.
"""
if not metadata:
print("No metadata found.")
return
# Compute padding based on longest key
key_width = max(len(str(k)) for k in metadata.keys()) + 2
separator = "=" * (key_width + 50)
print("\n" + separator)
print(" METADATA ".center(key_width + 50, "-"))
print(separator)
for k, v in sorted(metadata.items()):
print(f"{k:{key_width}}: {v}")
print(separator)
def check_timestamp_anomaly(self, file_path: str, metadata: dict) -> dict:
"""
Compare EXIF timestamps to filesystem timestamps.
Arguments:
file_path (str): Path to the image file.
metadata (dict): Metadata dict from ExifTool or Pillow.
Returns:
dict: Dictionary with anomaly info or empty if none.
"""
anomalies={} #initialize empty dictionary
#get EXIF timestamp
exif_time_str=metadata.get("DateTimeOriginal") or metadata.get("CreateDate") or metadata.get("DateTime")
if not exif_time_str:
anomalies["Timestamp"] = "No EXIF timestamp found."
return anomalies #exit early if no exif time :(
#parse EXIF timestamp (convert to python datetime object)
try:
exif_time = datetime.strptime(exif_time_str, "%Y:%m:%d %H:%M:%S")
except ValueError:
anomalies["Timestamp"] = f"Could not parse EXIF timestamp: {exif_time_str}"
return anomalies
#get filesystem timestamp
try:
file_stats = os.stat(file_path)
fs_modified = datetime.fromtimestamp(file_stats.st_mtime)
fs_created = datetime.fromtimestamp(file_stats.st_ctime)
except Exception as e:
anomalies["Error"] = f"Could not get filesystem timestamps: {e}"
return anomalies
#compare - check if difference is more than 100 seconds
#Modified mismatch → maybe the file was edited
if round(abs((fs_modified - exif_time).total_seconds()), 3) > 100:
anomalies["Modified Time Mismatch"] = {
"EXIF": exif_time.isoformat(),
"Filesystem": fs_modified.isoformat(),
"DeltaSeconds": abs((fs_modified - exif_time).total_seconds())
}
#Created mismatch → maybe the file was copied/moved long after creation
if round(abs((fs_created - exif_time).total_seconds()), 3) > 100:
anomalies["Created Time Mismatch"] = {
"EXIF": exif_time.isoformat(),
"Filesystem": fs_created.isoformat(),
"DeltaSeconds": abs((fs_created - exif_time).total_seconds())
}
return anomalies