2015-10-07 10:48:01 +02:00
|
|
|
"""
|
|
|
|
Author: Jaisen Mathai <jaisen@jmathai.com>
|
|
|
|
Photo package that handles all photo operations
|
|
|
|
"""
|
|
|
|
|
2015-12-11 08:07:01 +01:00
|
|
|
import imghdr
|
|
|
|
import LatLon
|
2015-10-07 10:48:01 +02:00
|
|
|
import os
|
2015-10-20 10:17:09 +02:00
|
|
|
import pyexiv2
|
2015-10-07 10:48:01 +02:00
|
|
|
import re
|
2015-10-21 08:51:14 +02:00
|
|
|
import subprocess
|
2015-10-07 10:48:01 +02:00
|
|
|
import time
|
|
|
|
|
2015-12-20 09:30:30 +01:00
|
|
|
from elodie import constants
|
2015-10-20 10:17:09 +02:00
|
|
|
from media import Media
|
|
|
|
from elodie import geolocation
|
2015-10-07 10:48:01 +02:00
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-07 10:48:01 +02:00
|
|
|
class Photo(Media):
|
2015-11-19 11:31:32 +01:00
|
|
|
__name__ = 'Photo'
|
2015-12-11 08:07:01 +01:00
|
|
|
extensions = ('jpg', 'jpeg', 'nef', 'dng', 'gif')
|
2015-10-07 10:48:01 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
@param, source, string, The fully qualified path to the photo file
|
|
|
|
"""
|
|
|
|
def __init__(self, source=None):
|
|
|
|
super(Photo, self).__init__(source)
|
|
|
|
|
|
|
|
# We only want to parse EXIF once so we store it here
|
|
|
|
self.exif = None
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-10-07 10:48:01 +02:00
|
|
|
"""
|
|
|
|
Get the duration of a photo in seconds.
|
|
|
|
Uses ffmpeg/ffprobe
|
|
|
|
|
|
|
|
@returns, string or None for a non-photo file
|
|
|
|
"""
|
|
|
|
def get_duration(self):
|
|
|
|
if(not self.is_valid()):
|
|
|
|
return None
|
|
|
|
|
|
|
|
source = self.source
|
2016-01-02 08:23:06 +01:00
|
|
|
result = subprocess.Popen(
|
|
|
|
['ffprobe', source],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT
|
|
|
|
)
|
2015-10-07 10:48:01 +02:00
|
|
|
for key in result.stdout.readlines():
|
|
|
|
if 'Duration' in key:
|
2016-01-02 08:23:06 +01:00
|
|
|
return re.search(
|
|
|
|
'(\d{2}:\d{2}.\d{2})',
|
|
|
|
key
|
|
|
|
).group(1).replace('.', ':')
|
2015-10-07 10:48:01 +02:00
|
|
|
return None
|
|
|
|
|
2015-12-11 08:07:01 +01:00
|
|
|
"""
|
|
|
|
Get latitude or longitude of photo from EXIF
|
|
|
|
|
|
|
|
@returns, float or None if not present in EXIF or a non-photo file
|
|
|
|
"""
|
|
|
|
def get_coordinate(self, type='latitude'):
|
|
|
|
if(not self.is_valid()):
|
|
|
|
return None
|
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
key = self.exif_map['latitude']
|
|
|
|
if(type == 'longitude'):
|
|
|
|
key = self.exif_map['longitude']
|
2015-12-11 08:07:01 +01:00
|
|
|
exif = self.get_exif()
|
|
|
|
|
|
|
|
if(key not in exif):
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
2016-01-02 08:23:06 +01:00
|
|
|
# this is a hack to get the proper direction by negating the
|
|
|
|
# values for S and W
|
2015-12-11 08:07:01 +01:00
|
|
|
latdir = 1
|
2016-01-02 08:23:06 +01:00
|
|
|
if(type == 'latitude' and str(exif[self.exif_map['latitude_ref']].value) == 'S'): # noqa
|
2015-12-11 08:07:01 +01:00
|
|
|
latdir = -1
|
2016-01-02 08:23:06 +01:00
|
|
|
|
2015-12-11 08:07:01 +01:00
|
|
|
londir = 1
|
2016-01-02 08:23:06 +01:00
|
|
|
if(type == 'longitude' and str(exif[self.exif_map['longitude_ref']].value) == 'W'): # noqa
|
2015-12-11 08:07:01 +01:00
|
|
|
londir = -1
|
|
|
|
|
|
|
|
coords = exif[key].value
|
2015-12-25 10:05:38 +01:00
|
|
|
if(type == 'latitude'):
|
2016-01-02 08:23:06 +01:00
|
|
|
lat_val = LatLon.Latitude(
|
|
|
|
degree=coords[0],
|
|
|
|
minute=coords[1],
|
|
|
|
second=coords[2]
|
|
|
|
)
|
|
|
|
return float(str(lat_val)) * latdir
|
2015-12-11 08:07:01 +01:00
|
|
|
else:
|
2016-01-02 08:23:06 +01:00
|
|
|
lon_val = LatLon.Longitude(
|
|
|
|
degree=coords[0],
|
|
|
|
minute=coords[1],
|
|
|
|
second=coords[2]
|
|
|
|
)
|
|
|
|
return float(str(lon_val)) * londir
|
2015-12-11 08:07:01 +01:00
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
"""
|
2015-12-12 09:43:49 +01:00
|
|
|
Get the date which the photo was taken.
|
|
|
|
The date value returned is defined by the min() of mtime and ctime.
|
|
|
|
|
|
|
|
@returns, time object or None for non-photo files or 0 timestamp
|
|
|
|
"""
|
|
|
|
def get_date_taken(self):
|
|
|
|
if(not self.is_valid()):
|
|
|
|
return None
|
|
|
|
|
|
|
|
source = self.source
|
2016-01-02 08:23:06 +01:00
|
|
|
seconds_since_epoch = min(os.path.getmtime(source), os.path.getctime(source)) # noqa
|
2015-12-12 09:43:49 +01:00
|
|
|
# We need to parse a string from EXIF into a timestamp.
|
2016-01-02 08:23:06 +01:00
|
|
|
# EXIF DateTimeOriginal and EXIF DateTime are both stored
|
|
|
|
# in %Y:%m:%d %H:%M:%S format
|
|
|
|
# we use date.strptime -> .timetuple -> time.mktime to do
|
|
|
|
# the conversion in the local timezone
|
2015-12-12 09:43:49 +01:00
|
|
|
# EXIF DateTime is already stored as a timestamp
|
2016-01-02 08:23:06 +01:00
|
|
|
# Sourced from https://github.com/photo/frontend/blob/master/src/libraries/models/Photo.php#L500 # noqa
|
2015-12-12 09:43:49 +01:00
|
|
|
exif = self.get_exif()
|
|
|
|
for key in self.exif_map['date_taken']:
|
|
|
|
try:
|
|
|
|
if(key in exif):
|
2016-01-02 08:23:06 +01:00
|
|
|
if(re.match('\d{4}(-|:)\d{2}(-|:)\d{2}', str(exif[key].value)) is not None): # noqa
|
|
|
|
seconds_since_epoch = time.mktime(exif[key].value.timetuple()) # noqa
|
|
|
|
break
|
2015-12-12 09:43:49 +01:00
|
|
|
except BaseException as e:
|
2016-01-02 08:23:06 +01:00
|
|
|
if(constants.debug is True):
|
2015-12-12 09:43:49 +01:00
|
|
|
print e
|
|
|
|
pass
|
|
|
|
|
|
|
|
if(seconds_since_epoch == 0):
|
|
|
|
return None
|
|
|
|
|
|
|
|
return time.gmtime(seconds_since_epoch)
|
|
|
|
|
|
|
|
"""
|
2016-01-02 08:23:06 +01:00
|
|
|
Check the file extension against valid file extensions as returned
|
|
|
|
by self.extensions
|
|
|
|
|
2015-12-11 08:07:01 +01:00
|
|
|
@returns, boolean
|
|
|
|
"""
|
|
|
|
def is_valid(self):
|
|
|
|
source = self.source
|
|
|
|
|
|
|
|
# gh-4 This checks if the source file is an image.
|
|
|
|
# It doesn't validate against the list of supported types.
|
|
|
|
if(imghdr.what(source) is None):
|
2016-01-02 08:23:06 +01:00
|
|
|
return False
|
2015-12-11 08:07:01 +01:00
|
|
|
|
|
|
|
return os.path.splitext(source)[1][1:].lower() in self.extensions
|
|
|
|
|
2015-10-20 10:17:09 +02:00
|
|
|
"""
|
|
|
|
Set the date/time a photo was taken
|
|
|
|
|
|
|
|
@param, time, datetime, datetime object of when the photo was taken
|
|
|
|
|
|
|
|
@returns, boolean
|
|
|
|
"""
|
2015-11-30 07:01:27 +01:00
|
|
|
def set_date_taken(self, time):
|
2015-10-20 10:17:09 +02:00
|
|
|
if(time is None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
source = self.source
|
|
|
|
exif_metadata = pyexiv2.ImageMetadata(source)
|
|
|
|
exif_metadata.read()
|
|
|
|
|
|
|
|
exif_metadata['Exif.Photo.DateTimeOriginal'].value = time
|
|
|
|
exif_metadata['Exif.Image.DateTime'].value = time
|
|
|
|
|
|
|
|
exif_metadata.write()
|
|
|
|
return True
|
|
|
|
|
|
|
|
"""
|
|
|
|
Set lat/lon for a photo
|
|
|
|
|
|
|
|
@param, latitude, float, Latitude of the file
|
|
|
|
@param, longitude, float, Longitude of the file
|
|
|
|
|
|
|
|
@returns, boolean
|
|
|
|
"""
|
|
|
|
def set_location(self, latitude, longitude):
|
|
|
|
if(latitude is None or longitude is None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
source = self.source
|
|
|
|
exif_metadata = pyexiv2.ImageMetadata(source)
|
|
|
|
exif_metadata.read()
|
|
|
|
|
2016-01-02 08:23:06 +01:00
|
|
|
exif_metadata['Exif.GPSInfo.GPSLatitude'] = geolocation.decimal_to_dms(latitude, False) # noqa
|
|
|
|
exif_metadata['Exif.GPSInfo.GPSLatitudeRef'] = pyexiv2.ExifTag('Exif.GPSInfo.GPSLatitudeRef', 'N' if latitude >= 0 else 'S') # noqa
|
|
|
|
exif_metadata['Exif.GPSInfo.GPSLongitude'] = geolocation.decimal_to_dms(longitude, False) # noqa
|
|
|
|
exif_metadata['Exif.GPSInfo.GPSLongitudeRef'] = pyexiv2.ExifTag('Exif.GPSInfo.GPSLongitudeRef', 'E' if longitude >= 0 else 'W') # noqa
|
2015-10-20 10:17:09 +02:00
|
|
|
|
|
|
|
exif_metadata.write()
|
|
|
|
return True
|
|
|
|
|
2015-10-28 08:19:21 +01:00
|
|
|
"""
|
2015-12-11 08:07:01 +01:00
|
|
|
Set title for a photo
|
2015-10-28 08:19:21 +01:00
|
|
|
|
|
|
|
@param, latitude, float, Latitude of the file
|
|
|
|
@param, longitude, float, Longitude of the file
|
|
|
|
|
|
|
|
@returns, boolean
|
|
|
|
"""
|
|
|
|
def set_title(self, title):
|
|
|
|
if(title is None):
|
|
|
|
return False
|
|
|
|
|
|
|
|
source = self.source
|
|
|
|
exif_metadata = pyexiv2.ImageMetadata(source)
|
|
|
|
exif_metadata.read()
|
|
|
|
|
|
|
|
exif_metadata['Xmp.dc.title'] = title
|
|
|
|
|
|
|
|
exif_metadata.write()
|
|
|
|
return True
|
|
|
|
|
2015-10-07 10:48:01 +02:00
|
|
|
"""
|
|
|
|
Static method to access static __valid_extensions variable.
|
|
|
|
|
|
|
|
@returns, tuple
|
|
|
|
"""
|
|
|
|
@classmethod
|
2016-01-02 18:34:43 +01:00
|
|
|
def get_valid_extensions(cls):
|
|
|
|
return cls.extensions
|