mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-28 18:10:33 +03:00
v1/v2 tag storage in IFD, legacy_api as a parameter to _saveitem, save both _tags_v* when saving as legacy api
This commit is contained in:
parent
a9396ab412
commit
e1236d702d
|
@ -244,15 +244,26 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
* self.tagtype = {} Key: numerical tiff tag number
|
* self.tagtype = {} Key: numerical tiff tag number
|
||||||
Value: integer corresponding to the data type from
|
Value: integer corresponding to the data type from
|
||||||
`TiffTags.TYPES`
|
`TiffTags.TYPES`
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
Documentation:
|
||||||
|
|
||||||
'internal'
|
'internal'
|
||||||
* self._tags = {} Key: numerical tiff tag number
|
* self._tags_v2 = {} Key: numerical tiff tag number
|
||||||
Value: decoded data, as tuple for multiple values
|
Value: decoded data, as tuple for multiple values
|
||||||
* self._tagdata = {} Key: numerical tiff tag number
|
* self._tagdata = {} Key: numerical tiff tag number
|
||||||
Value: undecoded byte string from file
|
Value: undecoded byte string from file
|
||||||
|
* self._tags_v1 = {} Key: numerical tiff tag number
|
||||||
|
Value: decoded data in the v1 format
|
||||||
|
|
||||||
Tags will be found in the private attributes self._tagdata, and in
|
Tags will be found in the private attributes self._tagdata, and in
|
||||||
self._tags once decoded.
|
self._tags_v2 once decoded.
|
||||||
|
|
||||||
|
If legacy_api is true, then decoded tags will be populated into both
|
||||||
|
_tags_v1 and _tags_v2. _Tags_v2 will be used if this IFD is used in
|
||||||
|
the TIFF save routine.
|
||||||
|
|
||||||
|
Tags will be read from tags_v1 if legacy_api == true.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, ifh=b"II\052\0\0\0\0\0", prefix=None):
|
def __init__(self, ifh=b"II\052\0\0\0\0\0", prefix=None):
|
||||||
|
@ -285,12 +296,11 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
|
|
||||||
@legacy_api.setter
|
@legacy_api.setter
|
||||||
def legacy_api(self, value):
|
def legacy_api(self, value):
|
||||||
if value != self._legacy_api:
|
raise Exception("Not allowing setting of legacy api")
|
||||||
self._tags.clear()
|
|
||||||
self._legacy_api = value
|
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
self._tags = {}
|
self._tags_v1 = {} # will remain empty if legacy_api is false
|
||||||
|
self._tags_v2 = {} # main tag storage
|
||||||
self._tagdata = {}
|
self._tagdata = {}
|
||||||
self.tagtype = {} # added 2008-06-05 by Florian Hoech
|
self.tagtype = {} # added 2008-06-05 by Florian Hoech
|
||||||
self._next = None
|
self._next = None
|
||||||
|
@ -312,27 +322,30 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
for code, value in self.items())
|
for code, value in self.items())
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(set(self._tagdata) | set(self._tags))
|
return len(set(self._tagdata) | set(self._tags_v2))
|
||||||
|
|
||||||
def __getitem__(self, tag):
|
def __getitem__(self, tag):
|
||||||
if tag not in self._tags: # unpack on the fly
|
if tag not in self._tags_v2: # unpack on the fly
|
||||||
data = self._tagdata[tag]
|
data = self._tagdata[tag]
|
||||||
typ = self.tagtype[tag]
|
typ = self.tagtype[tag]
|
||||||
size, handler = self._load_dispatch[typ]
|
size, handler = self._load_dispatch[typ]
|
||||||
self[tag] = handler(self, self.legacy_api, data) # check type
|
self[tag] = handler(self, self.legacy_api, data) # check type
|
||||||
val = self._tags[tag]
|
val = self._tags_v2[tag]
|
||||||
if self.legacy_api and not isinstance(val, (tuple, bytes)):
|
if self.legacy_api and not isinstance(val, (tuple, bytes)):
|
||||||
val = val,
|
val = val,
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def __contains__(self, tag):
|
def __contains__(self, tag):
|
||||||
return tag in self._tags or tag in self._tagdata
|
return tag in self._tags_v2 or tag in self._tagdata
|
||||||
|
|
||||||
if bytes is str:
|
if bytes is str:
|
||||||
def has_key(self, tag):
|
def has_key(self, tag):
|
||||||
return tag in self
|
return tag in self
|
||||||
|
|
||||||
def __setitem__(self, tag, value):
|
def __setitem__(self, tag, value):
|
||||||
|
self._setitem(tag, value, self.legacy_api)
|
||||||
|
|
||||||
|
def _setitem(self, tag, value, legacy_api):
|
||||||
basetypes = (Number, bytes, str)
|
basetypes = (Number, bytes, str)
|
||||||
if bytes is str:
|
if bytes is str:
|
||||||
basetypes += unicode,
|
basetypes += unicode,
|
||||||
|
@ -363,20 +376,25 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
if self.tagtype[tag] == 7 and bytes is not str:
|
if self.tagtype[tag] == 7 and bytes is not str:
|
||||||
values = [value.encode("ascii",'replace') if isinstance(value, str) else value
|
values = [value.encode("ascii",'replace') if isinstance(value, str) else value
|
||||||
for value in values]
|
for value in values]
|
||||||
|
|
||||||
values = tuple(info.cvt_enum(value) for value in values)
|
values = tuple(info.cvt_enum(value) for value in values)
|
||||||
|
|
||||||
|
dest = self._tags_v1 if legacy_api else self._tags_v2
|
||||||
|
|
||||||
if info.length == 1:
|
if info.length == 1:
|
||||||
if self.legacy_api and self.tagtype[tag] in [5, 10]:
|
if legacy_api and self.tagtype[tag] in [5, 10]:
|
||||||
values = values,
|
values = values,
|
||||||
self._tags[tag], = values
|
dest[tag], = values
|
||||||
else:
|
else:
|
||||||
self._tags[tag] = values
|
dest[tag] = values
|
||||||
|
|
||||||
def __delitem__(self, tag):
|
def __delitem__(self, tag):
|
||||||
self._tags.pop(tag, None)
|
self._tags_v2.pop(tag, None)
|
||||||
|
self._tags_v1.pop(tag, None)
|
||||||
self._tagdata.pop(tag, None)
|
self._tagdata.pop(tag, None)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter(set(self._tagdata) | set(self._tags))
|
return iter(set(self._tagdata) | set(self._tags_v2))
|
||||||
|
|
||||||
def _unpack(self, fmt, data):
|
def _unpack(self, fmt, data):
|
||||||
return struct.unpack(self._endian + fmt, data)
|
return struct.unpack(self._endian + fmt, data)
|
||||||
|
@ -536,15 +554,15 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
fp.write(self._prefix + self._pack("HL", 42, 8))
|
fp.write(self._prefix + self._pack("HL", 42, 8))
|
||||||
|
|
||||||
# FIXME What about tagdata?
|
# FIXME What about tagdata?
|
||||||
fp.write(self._pack("H", len(self._tags)))
|
fp.write(self._pack("H", len(self._tags_v2)))
|
||||||
|
|
||||||
entries = []
|
entries = []
|
||||||
offset = fp.tell() + len(self._tags) * 12 + 4
|
offset = fp.tell() + len(self._tags_v2) * 12 + 4
|
||||||
stripoffsets = None
|
stripoffsets = None
|
||||||
|
|
||||||
# pass 1: convert tags to binary format
|
# pass 1: convert tags to binary format
|
||||||
# always write tags in ascending order
|
# always write tags in ascending order
|
||||||
for tag, value in sorted(self._tags.items()):
|
for tag, value in sorted(self._tags_v2.items()):
|
||||||
if tag == STRIPOFFSETS:
|
if tag == STRIPOFFSETS:
|
||||||
stripoffsets = len(entries)
|
stripoffsets = len(entries)
|
||||||
typ = self.tagtype.get(tag)
|
typ = self.tagtype.get(tag)
|
||||||
|
@ -609,19 +627,63 @@ del _load_dispatch, _write_dispatch, idx, name
|
||||||
class ImageFileDirectory_v1(ImageFileDirectory_v2):
|
class ImageFileDirectory_v1(ImageFileDirectory_v2):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
ImageFileDirectory_v2.__init__(self, *args, **kwargs)
|
ImageFileDirectory_v2.__init__(self, *args, **kwargs)
|
||||||
self.legacy_api=True
|
self._legacy_api=True
|
||||||
#insert deprecation warning here.
|
#insert deprecation warning here.
|
||||||
|
|
||||||
tags = property(lambda self: self._tags)
|
tags = property(lambda self: self._tags_v1)
|
||||||
tagdata = property(lambda self: self._tagdata)
|
tagdata = property(lambda self: self._tagdata)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_v2(cls, original):
|
def from_v2(cls, original):
|
||||||
|
""" returns: ImageFileDirectory_v1
|
||||||
|
|
||||||
|
Returns an ImageFileDirectory_v1 instance with the same
|
||||||
|
data as is contained in the original ImageFileDirectory_v2
|
||||||
|
instance """
|
||||||
|
|
||||||
ifd = cls(prefix=original.prefix)
|
ifd = cls(prefix=original.prefix)
|
||||||
ifd._tagdata = original._tagdata
|
ifd._tagdata = original._tagdata
|
||||||
ifd.tagtype = original.tagtype
|
ifd.tagtype = original.tagtype
|
||||||
return ifd
|
return ifd
|
||||||
|
|
||||||
|
def to_v2(self):
|
||||||
|
""" returns: ImageFileDirectory_v2
|
||||||
|
|
||||||
|
Returns an ImageFileDirectory_v2 instance with the same
|
||||||
|
data as is contained in this ImageFileDirectory_v1 instance """
|
||||||
|
|
||||||
|
ifd = ImageFileDirectory_v2(prefix=self.prefix)
|
||||||
|
ifd._tagdata = dict(self._tagdata)
|
||||||
|
ifd.tagtype = dict(self.tagtype)
|
||||||
|
ifd._tags_v2 = dict(self._tags_v2)
|
||||||
|
return ifd
|
||||||
|
|
||||||
|
def __contains__(self, tag):
|
||||||
|
return tag in self._tags_v1 or tag in self._tagdata
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(set(self._tagdata) | set(self._tags_v1))
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(set(self._tagdata) | set(self._tags_v1))
|
||||||
|
|
||||||
|
def __setitem__(self, tag, value):
|
||||||
|
for legacy_api in (False,True):
|
||||||
|
self._setitem(tag, value, legacy_api)
|
||||||
|
|
||||||
|
def __getitem__(self, tag):
|
||||||
|
if tag not in self._tags_v1: # unpack on the fly
|
||||||
|
data = self._tagdata[tag]
|
||||||
|
typ = self.tagtype[tag]
|
||||||
|
size, handler = self._load_dispatch[typ]
|
||||||
|
for legacy in (False, True):
|
||||||
|
self._setitem(tag, handler(self, legacy, data), legacy)
|
||||||
|
val = self._tags_v1[tag]
|
||||||
|
if not isinstance(val, (tuple, bytes)):
|
||||||
|
val = val,
|
||||||
|
return val
|
||||||
|
|
||||||
|
|
||||||
# undone -- switch this pointer when IFD_LEGACY_API == False
|
# undone -- switch this pointer when IFD_LEGACY_API == False
|
||||||
ImageFileDirectory = ImageFileDirectory_v1
|
ImageFileDirectory = ImageFileDirectory_v1
|
||||||
|
|
||||||
|
@ -1076,6 +1138,8 @@ def _save(im, fp, filename):
|
||||||
info = im.encoderinfo.get("tiffinfo", {})
|
info = im.encoderinfo.get("tiffinfo", {})
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
print("Tiffinfo Keys: %s" % list(info))
|
print("Tiffinfo Keys: %s" % list(info))
|
||||||
|
if isinstance(info, ImageFileDirectory_v1):
|
||||||
|
info = info.to_v2()
|
||||||
for key in info:
|
for key in info:
|
||||||
ifd[key] = info.get(key)
|
ifd[key] = info.get(key)
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user