Decoding EXIF strings as UTF-8 with replace in the ifd_tag_to_dict

method.
This commit is contained in:
Joar Wandborg 2012-07-07 20:01:19 +02:00
parent d34757dc83
commit 8588505c6f

View File

@ -102,6 +102,10 @@ def clean_exif(exif):
def _ifd_tag_to_dict(tag):
'''
Takes an IFD tag object from the EXIF library and converts it to a dict
that can be stored as JSON in the database.
'''
data = {
'printable': tag.printable,
'tag': tag.tag,
@ -109,6 +113,11 @@ def _ifd_tag_to_dict(tag):
'field_offset': tag.field_offset,
'field_length': tag.field_length,
'values': None}
if isinstance(tag.printable, str):
# Force it to be decoded as UTF-8 so that it'll fit into the DB
data['printable'] = tag.printable.decode('utf8', 'replace')
if type(tag.values) == list:
data['values'] = []
for val in tag.values:
@ -117,6 +126,10 @@ def _ifd_tag_to_dict(tag):
_ratio_to_list(val))
else:
data['values'].append(val)
else:
if isinstance(tag.values, str):
# Force UTF-8, so that it fits into the DB
data['values'] = tag.values.decode('utf8', 'replace')
else:
data['values'] = tag.values