Lookup tag info in both _v2(info) and original(name only) dicts, delegate to lookup

This commit is contained in:
wiredfool 2015-12-29 22:02:11 +00:00
parent 3ac9396e8c
commit 80ab12bdc0
2 changed files with 19 additions and 5 deletions

View File

@ -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=" ")

View File

@ -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.
#