Fix logged tag name when loading Exif data by radarhere · Pull Request #7842 · python-pillow/Pillow

In main,

import logging
from PIL import Image, ExifTags
im = Image.open("/Users/andrewmurray/pillow/Pillow/Tests/images/exif_gps.jpg")
exif = im.getexif()

logging.basicConfig(level=logging.DEBUG)
exif.get_ifd(ExifTags.IFD.GPSInfo)

gives

DEBUG:PIL.TiffImagePlugin:tag: unknown (0) - type: byte (1) - value: b'\x00\x00\x00\x01'
DEBUG:PIL.TiffImagePlugin:tag: unknown (2) - type: rational (5) Tag Location: 392 - Data Location: 428 - value: b'\xff\xff\xff\xff\x00\x00\x00\x01'
DEBUG:PIL.TiffImagePlugin:tag: unknown (5) - type: byte (1) - value: b'\x01'
DEBUG:PIL.TiffImagePlugin:tag: unknown (29) - type: string (2) Tag Location: 416 - Data Location: 436 - value: b'1999:99:99 99:99:99\x00'
DEBUG:PIL.TiffImagePlugin:tag: unknown (30) - type: short (3) - value: b'\xff\xff'

This PR aims to improve the "tag: unknown" portion.

The logging can be found at

msg = f"tag: {tagname} ({tag}) - type: {typname} ({typ})"
try:
unit_size, handler = self._load_dispatch[typ]
except KeyError:
logger.debug("%s - unsupported type %s", msg, typ)
continue # ignore unsupported type
size = count * unit_size
if size > (8 if self._bigtiff else 4):
here = fp.tell()
(offset,) = self._unpack("Q" if self._bigtiff else "L", data)
msg += f" Tag Location: {here} - Data Location: {offset}"
fp.seek(offset)
data = ImageFile._safe_read(fp, size)
fp.seek(here)
else:
data = data[:size]
if len(data) != size:
warnings.warn(
"Possibly corrupt EXIF data. "
f"Expecting to read {size} bytes but only got {len(data)}."
f" Skipping tag {tag}"
)
logger.debug(msg)
continue
if not data:
logger.debug(msg)
continue
self._tagdata[tag] = data
self.tagtype[tag] = typ
msg += " - value: " + (
"<table: %d bytes>" % size if size > 32 else repr(data)
)
logger.debug(msg)

The problem is that Image.Exif isn't populating the group when creating the IFD

def __init__(self, ifh=b"II\052\0\0\0\0\0", prefix=None, group=None):

When it is changed to do so, you get a more helpful output.

DEBUG:PIL.TiffImagePlugin:tag: GPSVersionID (0) - type: byte (1) - value: b'\x00\x00\x00\x01'
DEBUG:PIL.TiffImagePlugin:tag: GPSLatitude (2) - type: rational (5) Tag Location: 392 - Data Location: 428 - value: b'\xff\xff\xff\xff\x00\x00\x00\x01'
DEBUG:PIL.TiffImagePlugin:tag: GPSAltitudeRef (5) - type: byte (1) - value: b'\x01'
DEBUG:PIL.TiffImagePlugin:tag: GPSDateStamp (29) - type: string (2) Tag Location: 416 - Data Location: 436 - value: b'1999:99:99 99:99:99\x00'
DEBUG:PIL.TiffImagePlugin:tag: GPSDifferential (30) - type: short (3) - value: b'\xff\xff'