Skip to content

Commit

Permalink
Added IFD enum
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Nov 27, 2022
1 parent 72372ad commit 24a5405
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
7 changes: 7 additions & 0 deletions docs/reference/ExifTags.rst
Expand Up @@ -31,6 +31,13 @@ which provide constants and clear-text names for various well-known EXIF tags.
>>> Interop(4096).name
'RelatedImageFileFormat'

.. py:data:: IFD
>>> from PIL.ExifTags import IFD
>>> IFD.Exif.value
34665
>>> IFD(34665).name
'Exif'

Two of these values are also exposed as dictionaries.

Expand Down
7 changes: 7 additions & 0 deletions src/PIL/ExifTags.py
Expand Up @@ -346,3 +346,10 @@ class Interop(IntEnum):
RelatedImageFileFormat = 4096
RelatedImageWidth = 4097
RleatedImageHeight = 4098


class IFD(IntEnum):
Exif = 34665
GPSInfo = 34853
Makernote = 37500
Interop = 40965
45 changes: 29 additions & 16 deletions src/PIL/Image.py
Expand Up @@ -47,7 +47,14 @@
# VERSION was removed in Pillow 6.0.0.
# PILLOW_VERSION was removed in Pillow 9.0.0.
# Use __version__ instead.
from . import ImageMode, TiffTags, UnidentifiedImageError, __version__, _plugins
from . import (
ExifTags,
ImageMode,
TiffTags,
UnidentifiedImageError,
__version__,
_plugins,
)
from ._binary import i32le, o32be, o32le
from ._deprecate import deprecate
from ._util import DeferredError, is_path
Expand Down Expand Up @@ -3598,14 +3605,16 @@ def _get_merged_dict(self):
merged_dict = dict(self)

# get EXIF extension
if 0x8769 in self:
ifd = self._get_ifd_dict(self[0x8769])
if ExifTags.IFD.Exif in self:
ifd = self._get_ifd_dict(self[ExifTags.IFD.Exif])
if ifd:
merged_dict.update(ifd)

# GPS
if 0x8825 in self:
merged_dict[0x8825] = self._get_ifd_dict(self[0x8825])
if ExifTags.IFD.GPSInfo in self:
merged_dict[ExifTags.IFD.GPSInfo] = self._get_ifd_dict(
self[ExifTags.IFD.GPSInfo]
)

return merged_dict

Expand All @@ -3615,30 +3624,34 @@ def tobytes(self, offset=8):
head = self._get_head()
ifd = TiffImagePlugin.ImageFileDirectory_v2(ifh=head)
for tag, value in self.items():
if tag in [0x8769, 0x8225, 0x8825] and not isinstance(value, dict):
if tag in [
ExifTags.IFD.Exif,
0x8225,
ExifTags.IFD.GPSInfo,
] and not isinstance(value, dict):
value = self.get_ifd(tag)
if (
tag == 0x8769
and 0xA005 in value
and not isinstance(value[0xA005], dict)
tag == ExifTags.IFD.Exif
and ExifTags.IFD.Interop in value
and not isinstance(value[ExifTags.IFD.Interop], dict)
):
value = value.copy()
value[0xA005] = self.get_ifd(0xA005)
value[ExifTags.IFD.Interop] = self.get_ifd(ExifTags.IFD.Interop)
ifd[tag] = value
return b"Exif\x00\x00" + head + ifd.tobytes(offset)

def get_ifd(self, tag):
if tag not in self._ifds:
if tag in [0x8769, 0x8825]:
if tag in [ExifTags.IFD.Exif, ExifTags.IFD.GPSInfo]:
# exif, gpsinfo
if tag in self:
self._ifds[tag] = self._get_ifd_dict(self[tag])
elif tag in [0xA005, 0x927C]:
elif tag in [ExifTags.IFD.Interop, ExifTags.IFD.Makernote]:
# interop, makernote
if 0x8769 not in self._ifds:
self.get_ifd(0x8769)
tag_data = self._ifds[0x8769][tag]
if tag == 0x927C:
if ExifTags.IFD.Exif not in self._ifds:
self.get_ifd(ExifTags.IFD.Exif)
tag_data = self._ifds[ExifTags.IFD.Exif][tag]
if tag == ExifTags.IFD.Makernote:
# makernote
from .TiffImagePlugin import ImageFileDirectory_v2

Expand Down
11 changes: 9 additions & 2 deletions src/PIL/MpoImagePlugin.py
Expand Up @@ -22,7 +22,14 @@
import os
import struct

from . import Image, ImageFile, ImageSequence, JpegImagePlugin, TiffImagePlugin
from . import (
ExifTags,
Image,
ImageFile,
ImageSequence,
JpegImagePlugin,
TiffImagePlugin,
)
from ._binary import i16be as i16
from ._binary import o32le

Expand Down Expand Up @@ -137,7 +144,7 @@ def seek(self, frame):

mptype = self.mpinfo[0xB002][frame]["Attribute"]["MPType"]
if mptype.startswith("Large Thumbnail"):
exif = self.getexif().get_ifd(0x8769)
exif = self.getexif().get_ifd(ExifTags.IFD.Exif)
if 40962 in exif and 40963 in exif:
self._size = (exif[40962], exif[40963])
elif "exif" in self.info:
Expand Down

0 comments on commit 24a5405

Please sign in to comment.