Skip to content

Commit

Permalink
Add setting to customize the EXIF datetime format (fix saimn#271)
Browse files Browse the repository at this point in the history
- The default format is now `%c`
- Fix documentation confusion between ``datetime`` and ``dateobj``
  • Loading branch information
saimn committed Jan 3, 2018
1 parent 3ccce61 commit c864aee
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~
Expand Down
11 changes: 9 additions & 2 deletions docs/themes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -136,3 +141,5 @@ templates. If available, you can use:
media.exif.gps.lat }}&lon={{ media.exif.long}}">Go to location</a>
{% endif %}
.. _datetime documentation: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
3 changes: 2 additions & 1 deletion sigal/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sigal/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions sigal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '',
Expand Down
4 changes: 4 additions & 0 deletions sigal/templates/sigal.conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tests/test_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c864aee

Please sign in to comment.