Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logged tag name when loading Exif data #7842

Merged
merged 2 commits into from Mar 11, 2024
Merged

Conversation

radarhere
Copy link
Member

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'

@radarhere radarhere changed the title Fix logged tag name when loading with getexif() Fix logged tag name when loading Exif data Feb 29, 2024
@radarhere radarhere added the Exif label Feb 29, 2024
@hugovk hugovk merged commit 9fec5d5 into python-pillow:main Mar 11, 2024
57 checks passed
@radarhere radarhere deleted the exif branch March 12, 2024 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants