From 80ab12bdc0d6b546107536fb65e0b9c837cd09aa Mon Sep 17 00:00:00 2001 From: wiredfool Date: Tue, 29 Dec 2015 22:02:11 +0000 Subject: [PATCH] Lookup tag info in both _v2(info) and original(name only) dicts, delegate to lookup --- PIL/TiffImagePlugin.py | 12 +++++++----- PIL/TiffTags.py | 12 ++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index 521c7c726..b502c5661 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -44,6 +44,7 @@ from __future__ import division, print_function from PIL import Image, ImageFile from PIL import ImagePalette from PIL import _binary +from PIL import TiffTags import collections from fractions import Fraction @@ -56,7 +57,8 @@ import struct import sys import warnings -from .TiffTags import TAGS_V2, TYPES, TagInfo +from .TiffTags import TYPES, TagInfo + __version__ = "1.3.5" DEBUG = False # Needs to be merged with the new logging approach. @@ -461,7 +463,7 @@ class ImageFileDirectory_v2(collections.MutableMapping): Returns the complete tag dictionary, with named tags where possible. """ - return dict((TAGS_V2.get(code, TagInfo()).name, value) + return dict((TiffTags.lookup(code).name, value) for code, value in self.items()) def __len__(self): @@ -493,7 +495,7 @@ class ImageFileDirectory_v2(collections.MutableMapping): if bytes is str: basetypes += unicode, - info = TAGS_V2.get(tag, TagInfo()) + info = TiffTags.lookup(tag) values = [value] if isinstance(value, basetypes) else value if tag not in self.tagtype: @@ -648,7 +650,7 @@ class ImageFileDirectory_v2(collections.MutableMapping): for i in range(self._unpack("H", self._ensure_read(fp, 2))[0]): tag, typ, count, data = self._unpack("HHL4s", self._ensure_read(fp, 12)) if DEBUG: - tagname = TAGS_V2.get(tag, TagInfo()).name + tagname = TiffTags.lookup(tag).name typname = TYPES.get(typ, "unknown") print("tag: %s (%d) - type: %s (%d)" % (tagname, tag, typname, typ), end=" ") @@ -716,7 +718,7 @@ class ImageFileDirectory_v2(collections.MutableMapping): values = value if isinstance(value, tuple) else (value,) data = self._write_dispatch[typ](self, *values) if DEBUG: - tagname = TAGS_V2.get(tag, TagInfo()).name + tagname = TiffTags.lookup(tag).name typname = TYPES.get(typ, "unknown") print("save: %s (%d) - type: %s (%d)" % (tagname, tag, typname, typ), end=" ") diff --git a/PIL/TiffTags.py b/PIL/TiffTags.py index d00164502..07710fdcc 100644 --- a/PIL/TiffTags.py +++ b/PIL/TiffTags.py @@ -30,6 +30,18 @@ class TagInfo(namedtuple("_TagInfo", "value name type length enum")): def cvt_enum(self, value): return self.enum.get(value, value) +def lookup(tag): + """ + :param tag: Integer tag number + :returns: Taginfo namedtuple, From the TAGS_V2 info if possible, + otherwise just populating the value and name from TAGS. + If the tag is not recognized, "unknown" is returned for the name + + """ + + return TAGS_V2.get(tag, TagInfo(tag, TAGS.get(tag, 'unknown'))) + + ## # Map tag numbers to tag info. #