exif_info: Retrieve EXIF information

This commit is contained in:
2014-08-25 22:37:22 +02:00
parent fb53d674a7
commit 4c19fb997b
3 changed files with 156 additions and 0 deletions

69
exif_info/README.md Normal file
View File

@@ -0,0 +1,69 @@
EXIF Info
==================
* Retrieve EXIF informations from pictures
##How to Use
1. Install the PIL (Python Imaging Library) libraries, please refer to http://pythonware.com/products/pil/ or your OS manual for instructions.
2. This plugin is an extension to the Gallery plugin, so first install it, and refer to its README for usage instructions.
3. Add 'exif_info' to the plugin list.
If you want, you can also specify in your configuration:
EXIF_INFO_DEFAULT = True/False
To set whether or not to retrieve by default the EXIF informations from your pictures. This setting can be overriden on a per article/album basis.
###Articles
Override on a per article/post basis the default behaviour by adding the following:
exifinfo: "True"
or
exifinfo: "False"
###Gallery Page
At this time the Gallery page is *not* supported.
##Examples
###article.html
{% if article.album %}
{% for image in article.galleryimages %}
{% if article.galleryexif and article.galleryexif.get(image) %}
<table>
{% if article.galleryexif.get(image).get("Model") %}
<tr><th colspan="4">{{article.galleryexif.get(image).get("Model")}}</th></tr>
{% endif %}
{% if article.galleryexif.get(image).get("LensModel") %}
<tr><th colspan="4">{{article.galleryexif.get(image).get("LensModel")}}</th></tr>
{% endif %}
<tr>
{% if article.galleryexif.get(image).get("ISOSpeedRatings") %}
<td>{{article.galleryexif.get(image).get("ISOSpeedRatings")}}</td>
{% endif %}
{% if article.galleryexif.get(image).get("FocalLength") %}
<td>{{article.galleryexif.get(image).get("FocalLength")}}mm</td>
{% endif %}
{% if article.galleryexif.get(image).get("FNumber") %}
<td>f/{{article.galleryexif.get(image).get("FNumber")}}</td>
{% endif %}
{% if article.galleryexif.get(image).get("ExposureTime") %}
<td>{{article.galleryexif.get(image).get("ExposureTime")}}</td>
{% endif %}
</tr>
</table>
{% endif %}
{% endfor %}
{% endif %}
##Reasoning
This was developped as an external plugin, instead of adapting the Gallery plugin because this relies on the PIL libraries to be installed, and working.
As this set of library may - or not - be available on a given platform, it seemed unreasonable to limit the Gallery plugin to the systems where it is.

1
exif_info/__init__.py Normal file
View File

@@ -0,0 +1 @@
from .exif_info import *

86
exif_info/exif_info.py Normal file
View File

@@ -0,0 +1,86 @@
import os
import re
from pelican import signals
from PIL import Image
from PIL.ExifTags import TAGS
def get_exif_data(fname):
"""Get embedded EXIF data from an image file."""
exif = {}
ret = {}
try:
img = Image.open(fname)
if hasattr( img, '_getexif' ):
exifinfo = img._getexif()
if exifinfo != None:
for tag, value in exifinfo.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
except IOError:
print 'IOERROR ' + fname
#print ret
# Keep and format the most interesting fields
for tag in ret:
if tag == "DateTimeOriginal":
exif[tag] = ret[tag]
if tag == "Model":
exif[tag] = ret[tag]
if tag == "LensModel":
exif[tag] = ret[tag]
if tag == "ISOSpeedRatings":
exif[tag] = ret[tag]
if tag == "FocalLength":
exif[tag] = "{:2.1f}".format(float(ret[tag][0]) / float(ret[tag][1]))
if tag == "FNumber":
exif[tag] = "{:2.1f}".format(float(ret[tag][0]) / float(ret[tag][1]))
if tag == "ExposureTime":
exif[tag] = str(ret[tag][0]) + "/" + str(ret[tag][1])
return exif
def add_exif_post(generator):
get_exif = generator.settings.get('EXIF_INFO_DEFAULT')
if get_exif == None:
get_exif = True;
contentpath = generator.settings.get('PATH')
gallerycontentpath = os.path.join(contentpath,'images/gallery')
for article in generator.articles:
if 'exifinfo' in article.metadata.keys():
if article.metadata.get('exifinfo'):
# Ignore anything which is not a capitalization variation of
# true/false
if article.metadata.get('exifinfo').lower() == "true":
get_exif = True;
if article.metadata.get('exifinfo').lower() == "false":
get_exif = False;
if get_exif:
if 'gallery' in article.metadata.keys():
album = article.metadata.get('gallery')
galleryexif = dict()
articlegallerypath=os.path.join(gallerycontentpath, album)
# If the gallery has not yet been generated generate one
if article.metadata.get('galleryimages'):
galleryimages = article.metadata.get('galleryimages');
else:
galleryimages = []
if(os.path.isdir(articlegallerypath)):
for i in os.listdir(articlegallerypath):
if os.path.isfile(os.path.join(os.path.join(gallerycontentpath, album), i)):
galleryimages.append(i)
# Retrieve the EXIF informations for all the images
for img in galleryimages:
galleryexif[img] = get_exif_data(articlegallerypath + "/" + img)
article.galleryexif = galleryexif
def register():
signals.article_generator_finalized.connect(add_exif_post)