diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/pyexiv2.py | 367 |
1 files changed, 1 insertions, 366 deletions
diff --git a/src/pyexiv2.py b/src/pyexiv2.py index 4fd5e6f..4ea904f 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -1493,374 +1493,9 @@ class ImageMetadata(object): class Image(libexiv2python.Image): """ - Provide convenient methods for the manipulation of EXIF, IPTC and XMP - metadata. - - Provide convenient methods for the manipulation of EXIF, IPTC and XMP - metadata embedded in image files such as JPEG and TIFF files, using Python's - built-in types and modules such as datetime. + DEPRECATED. DO NOT USE. """ - def __init__(self, filename): - f = filename - if f.__class__ is unicode: - f = f.encode('utf-8') - libexiv2python.Image.__init__(self, f) - self.__exifTagsDict = {} - self.__iptcTagsDict = {} - self.__exifCached = False - self.__iptcCached = False - - def __get_exif_tag(self, key): - """ - DOCME - """ - tag = ExifTag(*self.__getExifTag(key)) - return tag - - def __get_iptc_tag(self, key): - """ - DOCME - """ - tag = IptcTag(*self.__getIptcTag(key)) - return tag - - def __getExifTagValue(self, key): - """ - Get the value associated to a key in EXIF metadata. - - Get the value associated to a key in EXIF metadata. - Whenever possible, the value is typed using Python's built-in types or - modules such as datetime when the value is composed of a date and a time - (e.g. the EXIF tag 'Exif.Photo.DateTimeOriginal'). - - Keyword arguments: - key -- the EXIF key of the requested metadata tag - """ - tagType, tagValue = self.__getExifTag(key) - if tagType not in ('Byte', 'Ascii', 'Undefined'): - values = [ConvertToPythonType('Exif', tagType, x) for x in tagValue.split()] - if len(values) == 1: - return values[0] - else: - return tuple(values) - else: - return ConvertToPythonType('Exif', tagType, tagValue) - - def __setExifTagValue(self, key, value): - """ - Set the value associated to a key in EXIF metadata. - - Set the value associated to a key in EXIF metadata. - The new value passed should be typed using Python's built-in types or - modules such as datetime when the value is composed of a date and a time - (e.g. the EXIF tag 'Exif.Photo.DateTimeOriginal'), the method takes care - of converting it before setting the internal EXIF tag value. - - Keyword arguments: - key -- the EXIF key of the requested metadata tag - value -- the new value for the requested metadata tag - """ - valueType = value.__class__ - if valueType == int or valueType == long: - strVal = str(value) - elif valueType == datetime.datetime: - strVal = value.strftime('%Y:%m:%d %H:%M:%S') - elif valueType == list or valueType == tuple: - strVal = ' '.join([str(x) for x in value]) - else: - # Value must already be a string. - # Warning: no distinction is possible between values that really are - # strings (type 'Ascii') and those that are supposed to be sequences - # of bytes (type 'Undefined'), in which case value must be passed as - # a string correctly formatted, using utility function - # StringToUndefined(). - strVal = str(value) - typeName, oldValue = self.__setExifTag(key, strVal) - return typeName - - def __getIptcTagValue(self, key): - """ - Get the value(s) associated to a key in IPTC metadata. - - Get the value associated to a key in IPTC metadata. - Whenever possible, the value is typed using Python's built-in types or - modules such as date when the value represents a date (e.g. the IPTC tag - 'Iptc.Application2.DateCreated'). - If key represents a repeatable tag, a list of several values is - returned. If not, or if it has only one repetition, the list simply has - one element. - - Keyword arguments: - key -- the IPTC key of the requested metadata tag - """ - return [ConvertToPythonType('Iptc', *x) for x in self.__getIptcTag(key)] - - def __setIptcTagValue(self, key, value, index=0): - """ - Set the value associated to a key in IPTC metadata. - - Set the value associated to a key in IPTC metadata. - The new value passed should be typed using Python's built-in types or - modules such as datetime when the value contains a date or a time - (e.g. the IPTC tags 'Iptc.Application2.DateCreated' and - 'Iptc.Application2.TimeCreated'), the method takes care - of converting it before setting the internal IPTC tag value. - If key references a repeatable tag, the parameter index (starting from - 0 like a list index) is used to determine which of the repetitions is to - be set. In case of an index greater than the highest existing one, adds - a repetition of the tag. index defaults to 0 for (the majority of) - non-repeatable tags. - - Keyword arguments: - key -- the IPTC key of the requested metadata tag - value -- the new value for the requested metadata tag - index -- the index of the tag repetition to set (default value: 0) - """ - if (index < 0): - raise IndexError('Index must be greater than or equal to zero') - valueType = value.__class__ - if valueType == int or valueType == long: - strVal = str(value) - elif valueType == datetime.date: - strVal = value.strftime('%Y-%m-%d') - elif valueType == datetime.time: - # The only legal format for a time is '%H:%M:%S±%H:%M', - # but if the UTC offset is absent (format '%H:%M:%S'), the time can - # still be set (exiv2 is permissive). - strVal = value.strftime('%H:%M:%S%Z') - else: - # Value must already be a string. - # Warning: no distinction is possible between values that really are - # strings (type 'String') and those that are of type 'Undefined'. - # FIXME: for tags of type 'Undefined', this does not seem to work... - strVal = str(value) - typeName, oldValue = self.__setIptcTag(key, strVal, index) - return typeName - - def __getitem__(self, key): - """ - Read access implementation of the [] operator on Image objects. - - Get the value associated to a key in EXIF/IPTC metadata. - The value is cached in an internal dictionary for later accesses. - - Whenever possible, the value is typed using Python's built-in types or - modules such as datetime when the value is composed of a date and a time - (e.g. the EXIF tag 'Exif.Photo.DateTimeOriginal') or date when the value - represents a date (e.g. the IPTC tag 'Iptc.Application2.DateCreated'). - - If key references a repeatable tag (IPTC only), a list of several values - is returned. If not, or if it has only one repetition, the list simply - has one element. - - Keyword arguments: - key -- the [EXIF|IPTC] key of the requested metadata tag - """ - if key.__class__ is not str: - raise TypeError('Key must be of type string') - tagFamily = key[:4] - if tagFamily == 'Exif': - try: - return self.__exifTagsDict[key] - except KeyError: - value = self.__getExifTagValue(key) - self.__exifTagsDict[key] = value - return value - elif tagFamily == 'Iptc': - try: - return self.__iptcTagsDict[key] - except KeyError: - value = self.__getIptcTagValue(key) - if len(value) == 1: - value = value[0] - elif len(value) > 1: - value = tuple(value) - self.__iptcTagsDict[key] = value - return value - else: - # This is exiv2's standard error message, all futures changes on - # exiv2's side should be reflected here. - # As a future development, consider i18n for error messages. - raise IndexError("Invalid key `" + key + "'") - - def __setitem__(self, key, value): - """ - Write access implementation of the [] operator on Image objects. - - Set the value associated to a key in EXIF/IPTC metadata. - The value is cached in an internal dictionary for later accesses. - - The new value passed should be typed using Python's built-in types or - modules such as datetime when the value contains a date and a time - (e.g. the EXIF tag 'Exif.Photo.DateTimeOriginal' or the IPTC tags - 'Iptc.Application2.DateCreated' and 'Iptc.Application2.TimeCreated'), - the method takes care of converting it before setting the internal tag - value. - - If key references a repeatable tag (IPTC only), value can be a list of - values (the new values will overwrite the old ones, and an empty list of - values will unset the tag). - - Keyword arguments: - key -- the [EXIF|IPTC] key of the requested metadata tag - value -- the new value for the requested metadata tag - """ - if key.__class__ is not str: - raise TypeError('Key must be of type string') - tagFamily = key[:4] - if tagFamily == 'Exif': - if value is not None: - # For datetime objects, microseconds are not supported by the - # EXIF specification, so truncate them if present. - if value.__class__ is datetime.datetime: - value = value.replace(microsecond=0) - - typeName = self.__setExifTagValue(key, value) - self.__exifTagsDict[key] = ConvertToPythonType(tagFamily, typeName, str(value)) - else: - self.__deleteExifTag(key) - if self.__exifTagsDict.has_key(key): - del self.__exifTagsDict[key] - elif tagFamily == 'Iptc': - # The case of IPTC tags is a bit trickier since some tags are - # repeatable. To simplify the process, parameter 'value' is - # transformed into a tuple if it is not already one and then each of - # its values is processed (set, that is) in a loop. - newValues = value - if newValues is None: - # Setting the value to None does not really make sense, but can - # in a way be seen as equivalent to deleting it, so this - # behaviour is simulated by providing an empty list for 'value'. - newValues = () - if newValues.__class__ is not tuple: - if newValues.__class__ is list: - # For flexibility, passing a list instead of a tuple works - newValues = tuple(newValues) - else: - # Interpret the value as a single element - newValues = (newValues,) - try: - oldValues = self.__iptcTagsDict[key] - if oldValues.__class__ is not tuple: - oldValues = (oldValues,) - except KeyError: - # The tag is not cached yet - try: - oldValues = self.__getitem__(key) - except KeyError: - # The tag is not set - oldValues = () - - # For time objects, microseconds are not supported by the IPTC - # specification, so truncate them if present. - tempNewValues = [] - for newValue in newValues: - if newValue.__class__ is datetime.time: - tempNewValues.append(newValue.replace(microsecond=0)) - else: - tempNewValues.append(newValue) - newValues = tuple(tempNewValues) - - # This loop processes the values one by one. There are 3 cases: - # * if the two tuples are of the exact same size, each item in - # oldValues is replaced by its new value in newValues; - # * if newValues is longer than oldValues, each item in oldValues - # is replaced by its new value in newValues and the new items - # are appended at the end of oldValues; - # * if newValues is shorter than oldValues, each item in newValues - # replaces the corresponding one in oldValues and the trailing - # extra items in oldValues are deleted. - for i in xrange(max(len(oldValues), len(newValues))): - try: - typeName = self.__setIptcTagValue(key, newValues[i], i) - except IndexError: - try: - self.__deleteIptcTag(key, min(len(oldValues), len(newValues))) - except KeyError: - pass - if len(newValues) > 0: - if len(newValues) == 1: - newValues = newValues[0] - self.__iptcTagsDict[key] = tuple([ConvertToPythonType(tagFamily, typeName, str(v)) for v in newValues]) - else: - if self.__iptcTagsDict.has_key(key): - del self.__iptcTagsDict[key] - else: - raise IndexError("Invalid key `" + key + "'") - - def __delitem__(self, key): - """ - Implementation of the del operator for deletion on Image objects. - - Delete the value associated to a key in EXIF/IPTC metadata. - - If key references a repeatable tag (IPTC only), all the associated - values will be deleted. - - Keyword arguments: - key -- the [EXIF|IPTC] key of the requested metadata tag - """ - if key.__class__ is not str: - raise TypeError('Key must be of type string') - tagFamily = key[:4] - if tagFamily == 'Exif': - self.__deleteExifTag(key) - if self.__exifTagsDict.has_key(key): - del self.__exifTagsDict[key] - elif tagFamily == 'Iptc': - try: - oldValues = self.__iptcTagsDict[key] - except KeyError: - oldValues = self.__getIptcTag(key) - for i in xrange(len(oldValues)): - self.__deleteIptcTag(key, 0) - if self.__iptcTagsDict.has_key(key): - del self.__iptcTagsDict[key] - else: - raise IndexError("Invalid key `" + key + "'") - - def cacheAllExifTags(self): - """ - Cache the EXIF tag values for faster subsequent access. - - Read the values of all the EXIF tags in the image and cache them in an - internal dictionary so as to speed up subsequent accesses. - """ - if not self.__exifCached: - for key in self.exifKeys(): - self[key] - self.__exifCached = True - - def cacheAllIptcTags(self): - """ - Cache the IPTC tag values for faster subsequent access. - - Read the values of all the IPTC tags in the image and cache them in an - internal dictionary so as to speed up subsequent accesses. - """ - if not self.__iptcCached: - for key in self.iptcKeys(): - self[key] - self.__iptcCached = True - - def interpretedExifValue(self, key): - """ - Get the interpreted value of an EXIF tag as presented by the exiv2 tool. - - For EXIF tags, the exiv2 command-line tool is capable of displaying - user-friendly interpreted values, such as 'top, left' for the - 'Exif.Image.Orientation' tag when it has value '1'. This method always - returns a string containing this interpreted value for a given tag. - Warning: calling this method will not cache the value in the internal - dictionary. - - Keyword arguments: - key -- the EXIF key of the requested metadata tag - """ - # This method was added as a requirement tracked by bug #147534 - return self.__getExifTagToString(key) - def copyMetadataTo(self, destImage): # TODO: add optional parameters exif=True, iptc=True, xmp=True, so that # one can choose to copy only part of the metadata. |