Browse Source

Add setting to customize the EXIF datetime format (fix #271)

- The default format is now `%c`
- Fix documentation confusion between ``datetime`` and ``dateobj``
pull/282/head
Simon Conseil 8 years ago
parent
commit
1e96a7c939
  1. 2
      docs/changelog.rst
  2. 11
      docs/themes.rst
  3. 3
      sigal/gallery.py
  4. 4
      sigal/image.py
  5. 1
      sigal/settings.py
  6. 4
      sigal/templates/sigal.conf.py
  7. 3
      tests/test_gallery.py
  8. 4
      tests/test_image.py

2
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
~~~~~~~~~~~~~

11
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</a>
{% endif %}
.. _datetime documentation: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

3
sigal/gallery.py

@ -159,7 +159,8 @@ class Image(Media):
@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

4
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)

1
sigal/settings.py

@ -37,6 +37,7 @@ _DEFAULT_CONFIG = {
'autorotate_images': True,
'colorbox_column_size': 4,
'copy_exif_data': False,
'datetime_format': '%c',
'destination': '_build',
'files_to_copy': (),
'google_analytics': '',

4
sigal/templates/sigal.conf.py

@ -69,6 +69,10 @@ img_size = (800, 600)
# 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,

3
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')

4
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:

Loading…
Cancel
Save