gh-74 Check for existing EXIF field before writing and create if needed. Add test case.
This commit is contained in:
parent
03b7c750a1
commit
c55b14e6c4
|
@ -171,8 +171,13 @@ class Photo(Media):
|
||||||
exif_metadata = pyexiv2.ImageMetadata(source)
|
exif_metadata = pyexiv2.ImageMetadata(source)
|
||||||
exif_metadata.read()
|
exif_metadata.read()
|
||||||
|
|
||||||
exif_metadata['Exif.Photo.DateTimeOriginal'].value = time
|
# Writing exif with pyexiv2 differs if the key already exists so we
|
||||||
exif_metadata['Exif.Image.DateTime'].value = time
|
# 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()
|
exif_metadata.write()
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import shutil
|
|
||||||
import string
|
import string
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
|
|
@ -116,6 +116,28 @@ def test_is_not_valid():
|
||||||
|
|
||||||
assert not photo.is_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():
|
def test_set_date_taken():
|
||||||
temporary_folder, folder = helper.create_working_folder()
|
temporary_folder, folder = helper.create_working_folder()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue