Browse Source

Insert 'exif' and 'raw_exif' keys into media_ctx

pull/25/head
Matthias Vogelgesang 13 years ago
parent
commit
c0c9b29622
  1. 22
      sigal/image.py
  2. 4
      sigal/writer.py
  3. 8
      tests/test_image.py

22
sigal/image.py

@ -99,21 +99,15 @@ def add_copyright(img, text):
draw.text((5, img.size[1] - 15), '\xa9 ' + text)
def _get_exif_data(exif):
if exif:
return dict((TAGS.get(tag, tag), value)
for (tag, value) in exif.items())
return {}
def get_exif_tags(source):
"""Read EXIF tags from file @source and return a dictionary containing the
EXIF data."""
"""Read EXIF tags from file @source and return a tuple of two dictionaries,
the first one containing the raw EXIF data, the second one a simplified
version with common tags."""
logger = logging.getLogger(__name__)
if not '.jpg' in source.lower():
return {}
return (None, None)
img = PILImage.open(source)
@ -122,8 +116,9 @@ def get_exif_tags(source):
except (TypeError, IOError):
exif = None
logger.warning(u'Could not read EXIF data from {0}'.format(source))
return (None, None)
data = _get_exif_data(exif)
data = dict((TAGS.get(t, t), v) for (t, v) in exif.items()) if exif else {}
simple = {}
# Provide more accessible tags in the 'simple' key
@ -141,7 +136,4 @@ def get_exif_tags(source):
if 'ISOSpeedRatings' in data:
simple['iso'] = data['ISOSpeedRatings']
if simple:
data['simple'] = simple
return data
return (data, simple)

4
sigal/writer.py

@ -152,7 +152,9 @@ class Writer(object):
media_ctx['file'] = i
file_path = os.path.join(path, i)
media_ctx['exif'] = sigal.image.get_exif_tags(file_path)
raw, simple = sigal.image.get_exif_tags(file_path)
media_ctx['raw_exif'] = raw
media_ctx['exif'] = simple
else:
media_ctx['type'] = 'vid'
media_ctx['file'] = base + '.webm'

8
tests/test_image.py

@ -57,8 +57,10 @@ def test_exif_copy(tmpdir):
dst_file = str(tmpdir.join(test_image))
generate_image(src_file, dst_file, (300, 400), None, copy_exif_data=True)
tags = get_exif_tags(dst_file)
assert tags['simple']['iso'] == 50
raw, simple = get_exif_tags(dst_file)
assert simple['iso'] == 50
generate_image(src_file, dst_file, (300, 400), None, copy_exif_data=False)
assert not get_exif_tags(dst_file)
raw, simple = get_exif_tags(dst_file)
assert not raw
assert not simple

Loading…
Cancel
Save