gh-74 Check for existing EXIF field before writing and create if needed. Add test case.

This commit is contained in:
Jaisen Mathai 2016-01-12 20:44:15 -08:00
parent 03b7c750a1
commit c55b14e6c4
3 changed files with 29 additions and 3 deletions

View File

@ -171,8 +171,13 @@ class Photo(Media):
exif_metadata = pyexiv2.ImageMetadata(source)
exif_metadata.read()
exif_metadata['Exif.Photo.DateTimeOriginal'].value = time
exif_metadata['Exif.Image.DateTime'].value = time
# Writing exif with pyexiv2 differs if the key already exists so we
# handle both cases here.
for key in ['Exif.Photo.DateTimeOriginal', 'Exif.Image.DateTime']:
if(key in exif_metadata):
exif_metadata[key].value = time
else:
exif_metadata[key] = pyexiv2.ExifTag(key, time)
exif_metadata.write()
return True

View File

@ -1,7 +1,6 @@
import hashlib
import os
import random
import shutil
import string
import tempfile

View File

@ -116,6 +116,28 @@ def test_is_not_valid():
assert not photo.is_valid()
def test_set_date_taken_with_missing_datetimeoriginal():
# When datetimeoriginal (or other key) is missing we have to add it gh-74
# https://github.com/jmathai/elodie/issues/74
temporary_folder, folder = helper.create_working_folder()
origin = '%s/photo.jpg' % folder
shutil.copyfile(helper.get_file('no-exif.jpg'), origin)
photo = Photo(origin)
status = photo.set_date_taken(datetime.datetime(2013, 9, 30, 7, 6, 5))
assert status == True, status
photo_new = Photo(origin)
metadata = photo_new.get_metadata()
date_taken = metadata['date_taken']
shutil.rmtree(folder)
assert date_taken == (2013, 9, 30, 7, 6, 5, 0, 273, 0), metadata['date_taken']
def test_set_date_taken():
temporary_folder, folder = helper.create_working_folder()