diff --git a/docs/changelog.rst b/docs/changelog.rst index 6698cd0c..3c39612a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -15,6 +15,8 @@ Released on 2017-xx-xx. - Added random thumbnail property for album [:issue:`241`]. - Improve CSP compatibility with colorbox theme [:issue:`245`]. - Set html lang attribute based upon locale [:issue:`257`]. +- New setting ``datetime_format`` to customize the EXIF datetime format + [:issue:`271`]. Version 1.3.0 ~~~~~~~~~~~~~ diff --git a/docs/themes.rst b/docs/themes.rst index 3f31260d..763397f1 100644 --- a/docs/themes.rst +++ b/docs/themes.rst @@ -110,13 +110,18 @@ templates. If available, you can use: The aperture value given as an F-number and formatted as a decimal. ``media.exif.datetime`` + The time the image was *taken*. It is formatted with the + ``datetime_format`` setting, which is ``%c`` by default. + See Python's `datetime documentation`_ for a list of all possible values. + +``media.exif.dateobj`` The time the image was *taken*. It is a datetime object, that can be formatted with ``strftime``: .. code-block:: jinja - {% if media.exif.datetime %} - {{ media.exif.datetime.strftime('%A, %d. %B %Y') }} + {% if media.exif.dateobj %} + {{ media.exif.dateobj.strftime('%A, %d. %B %Y') }} {% endif %} This will output something like "Monday, 25. June 2013", depending on your @@ -136,3 +141,5 @@ templates. If available, you can use: media.exif.gps.lat }}&lon={{ media.exif.long}}">Go to location {% endif %} + +.. _datetime documentation: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior diff --git a/sigal/gallery.py b/sigal/gallery.py index cfdd746e..6d8d4606 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -159,7 +159,8 @@ def date(self): @cached_property def exif(self): - return (get_exif_tags(self.raw_exif) + datetime_format = self.settings['datetime_format'] + return (get_exif_tags(self.raw_exif, datetime_format=datetime_format) if self.raw_exif and self.ext in ('.jpg', '.jpeg') else None) @cached_property diff --git a/sigal/image.py b/sigal/image.py index 6ace65ac..9ef36e0f 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -243,7 +243,7 @@ def dms_to_degrees(v): return d + (m / 60.0) + (s / 3600.0) -def get_exif_tags(data): +def get_exif_tags(data, datetime_format='%c'): """Make a simplified version with common tags from raw EXIF data.""" logger = logging.getLogger(__name__) @@ -310,7 +310,7 @@ def get_exif_tags(data): try: simple['dateobj'] = datetime.strptime(date, '%Y:%m:%d %H:%M:%S') - dt = simple['dateobj'].strftime('%A, %d. %B %Y') + dt = simple['dateobj'].strftime(datetime_format) simple['datetime'] = dt.decode('utf8') if compat.PY2 else dt except (ValueError, TypeError) as e: logger.info(u'Could not parse DateTimeOriginal: %s', e) diff --git a/sigal/settings.py b/sigal/settings.py index eaf5a5bc..111330df 100644 --- a/sigal/settings.py +++ b/sigal/settings.py @@ -37,6 +37,7 @@ 'autorotate_images': True, 'colorbox_column_size': 4, 'copy_exif_data': False, + 'datetime_format': '%c', 'destination': '_build', 'files_to_copy': (), 'google_analytics': '', diff --git a/sigal/templates/sigal.conf.py b/sigal/templates/sigal.conf.py index 773f7095..3b743aaa 100644 --- a/sigal/templates/sigal.conf.py +++ b/sigal/templates/sigal.conf.py @@ -69,6 +69,10 @@ # If True, EXIF data from the original image is copied to the resized image # copy_exif_data = False +# Python's datetime format string used for the EXIF date formatting +# https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior +# datetime_format = '%c' + # Jpeg options # jpg_options = {'quality': 85, # 'optimize': True, diff --git a/tests/test_gallery.py b/tests/test_gallery.py index 013499d7..0e1647ec 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -113,9 +113,10 @@ def test_media_orig(settings, tmpdir): def test_image(settings, tmpdir): settings['destination'] = str(tmpdir) + settings['datetime_format'] = '%d/%m/%Y' m = Image('11.jpg', 'dir1/test1', settings) assert m.date == datetime.datetime(2006, 1, 22, 10, 32, 42) - assert m.exif['datetime'] == u'Sunday, 22. January 2006' + assert m.exif['datetime'] == u'22/01/2006' os.makedirs(join(settings['destination'], 'dir1', 'test1', 'thumbnails')) assert m.thumbnail == join('thumbnails', '11.tn.jpg') diff --git a/tests/test_image.py b/tests/test_image.py index d6e2b42f..b2f8c92d 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -135,12 +135,12 @@ def test_get_exif_tags(): src_file = os.path.join(CURRENT_DIR, 'sample', 'pictures', 'dir1', 'test1', test_image) data = get_exif_data(src_file) - simple = get_exif_tags(data) + simple = get_exif_tags(data, datetime_format='%d/%m/%Y') assert simple['fstop'] == 3.9 assert simple['focal'] == 12.0 assert simple['iso'] == 50 assert simple['Make'] == 'NIKON' - assert simple['datetime'] == 'Sunday, 22. January 2006' + assert simple['datetime'] == '22/01/2006' if PIL.PILLOW_VERSION == '3.0.0': assert simple['exposure'] == 0.00100603 else: