Fix EXIF viewer breaking on binary MakerNote fields
Pentax and other cameras have binary EXIF fields (MakerNote, etc.) that contain raw bytes. The previous code used errors="replace" which still produced strings with replacement characters that broke JSON parsing. Now properly detect non-printable binary data and display as "<N bytes binary>" instead. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -66,9 +66,15 @@ def read_image_exif(image_data: bytes) -> dict:
|
||||
# Convert bytes to string if possible
|
||||
elif isinstance(value, bytes):
|
||||
try:
|
||||
result[tag] = value.decode("utf-8", errors="replace").strip("\x00")
|
||||
except Exception:
|
||||
result[tag] = f"<{len(value)} bytes>"
|
||||
# Try to decode as ASCII/UTF-8 text
|
||||
decoded = value.decode("utf-8", errors="strict").strip("\x00")
|
||||
# Only keep if it looks like printable text
|
||||
if decoded.isprintable() or all(c.isspace() or c.isprintable() for c in decoded):
|
||||
result[tag] = decoded
|
||||
else:
|
||||
result[tag] = f"<{len(value)} bytes binary>"
|
||||
except (UnicodeDecodeError, Exception):
|
||||
result[tag] = f"<{len(value)} bytes binary>"
|
||||
# Handle tuples of IFDRational
|
||||
elif isinstance(value, tuple) and value and hasattr(value[0], "numerator"):
|
||||
result[tag] = [float(v) for v in value]
|
||||
|
||||
Reference in New Issue
Block a user