Versioned interface for TiffTags

This commit is contained in:
wiredfool 2015-09-13 15:15:13 +01:00
parent f3ab9b9f81
commit 4596df45c1
3 changed files with 20 additions and 12 deletions

View File

@ -55,7 +55,7 @@ import struct
import sys import sys
import warnings import warnings
from .TiffTags import TAGS, TYPES, TagInfo from .TiffTags import TAGS_V2, TYPES, TagInfo
__version__ = "1.3.5" __version__ = "1.3.5"
DEBUG = False # Needs to be merged with the new logging approach. DEBUG = False # Needs to be merged with the new logging approach.
@ -318,7 +318,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
""" """
Returns the complete tag dictionary, with named tags where possible. Returns the complete tag dictionary, with named tags where possible.
""" """
return dict((TAGS.get(code, TagInfo()).name, value) return dict((TAGS_V2.get(code, TagInfo()).name, value)
for code, value in self.items()) for code, value in self.items())
def __len__(self): def __len__(self):
@ -350,7 +350,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
if bytes is str: if bytes is str:
basetypes += unicode, basetypes += unicode,
info = TAGS.get(tag, TagInfo()) info = TAGS_V2.get(tag, TagInfo())
values = [value] if isinstance(value, basetypes) else value values = [value] if isinstance(value, basetypes) else value
if tag not in self.tagtype: if tag not in self.tagtype:
@ -503,7 +503,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
for i in range(self._unpack("H", self._ensure_read(fp,2))[0]): 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)) tag, typ, count, data = self._unpack("HHL4s", self._ensure_read(fp,12))
if DEBUG: if DEBUG:
tagname = TAGS.get(tag, TagInfo()).name tagname = TAGS_V2.get(tag, TagInfo()).name
typname = TYPES.get(typ, "unknown") typname = TYPES.get(typ, "unknown")
print("tag: %s (%d) - type: %s (%d)" % print("tag: %s (%d) - type: %s (%d)" %
(tagname, tag, typname, typ), end=" ") (tagname, tag, typname, typ), end=" ")
@ -571,7 +571,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
values = value if isinstance(value, tuple) else (value,) values = value if isinstance(value, tuple) else (value,)
data = self._write_dispatch[typ](self, *values) data = self._write_dispatch[typ](self, *values)
if DEBUG: if DEBUG:
tagname = TAGS.get(tag, TagInfo()).name tagname = TAGS_V2.get(tag, TagInfo()).name
typname = TYPES.get(typ, "unknown") typname = TYPES.get(typ, "unknown")
print("save: %s (%d) - type: %s (%d)" % print("save: %s (%d) - type: %s (%d)" %
(tagname, tag, typname, typ), end=" ") (tagname, tag, typname, typ), end=" ")

View File

@ -19,6 +19,9 @@
from collections import namedtuple from collections import namedtuple
# Legacy Tags structure
TAGS = {}
class TagInfo(namedtuple("_TagInfo", "value name type length enum")): class TagInfo(namedtuple("_TagInfo", "value name type length enum")):
__slots__ = [] __slots__ = []
@ -34,7 +37,7 @@ class TagInfo(namedtuple("_TagInfo", "value name type length enum")):
# #
# id: (Name, Type, Length, enum_values) # id: (Name, Type, Length, enum_values)
# #
TAGS = { TAGS_V2 = {
254: ("NewSubfileType", 4, 1), 254: ("NewSubfileType", 4, 1),
255: ("SubfileType", 3, 1), 255: ("SubfileType", 3, 1),
@ -160,12 +163,17 @@ TAGS = {
50839: ("ImageJMetaData", 7, 1) 50839: ("ImageJMetaData", 7, 1)
} }
def _populate():
for k, v in TAGS_V2.items():
# Populate legacy structure.
TAGS[k] = v[0]
if len(v) == 4:
for sk,sv in v[3].items():
TAGS[(k,sk)] = sv
TAGS_V2[k] = TagInfo(k, *v)
for k, v in TAGS.items(): _populate()
TAGS[k] = TagInfo(k, *v)
del k, v
## ##
# Map type numbers to type names -- defined in ImageFileDirectory. # Map type numbers to type names -- defined in ImageFileDirectory.

View File

@ -4,7 +4,7 @@ from helper import unittest, PillowTestCase, hopper
from PIL import Image, TiffImagePlugin, TiffTags from PIL import Image, TiffImagePlugin, TiffTags
tag_ids = dict((info.name, info.value) for info in TiffTags.TAGS.values()) tag_ids = dict((info.name, info.value) for info in TiffTags.TAGS_V2.values())
class TestFileTiffMetadata(PillowTestCase): class TestFileTiffMetadata(PillowTestCase):