plugin: import photos
diff --git a/theme/plugins/assets/__pycache__/__init__.cpython-35.pyc b/theme/plugins/assets/__pycache__/__init__.cpython-35.pyc
new file mode 100644
index 0000000..581a1c8
--- /dev/null
+++ b/theme/plugins/assets/__pycache__/__init__.cpython-35.pyc
Binary files differ
diff --git a/theme/plugins/assets/__pycache__/assets.cpython-35.pyc b/theme/plugins/assets/__pycache__/assets.cpython-35.pyc
new file mode 100644
index 0000000..fde1266
--- /dev/null
+++ b/theme/plugins/assets/__pycache__/assets.cpython-35.pyc
Binary files differ
diff --git a/theme/plugins/photos/README.md b/theme/plugins/photos/README.md
new file mode 100644
index 0000000..411265f
--- /dev/null
+++ b/theme/plugins/photos/README.md
@@ -0,0 +1,291 @@
+# Photos
+
+Use Photos to add a photo or a gallery of photos to an article, or to include photos in the body text. Photos are kept separately, as an organized library of high resolution photos, and resized as needed.
+
+## How to install and configure
+
+The plug-in requires `Pillow`: the Python Imaging Library and optionally `Piexif`, whose installation are outside the scope of this document.
+
+The plug-in resizes the referred photos, and generates thumbnails for galleries and associated photos, based on the following configuration and default values:
+
+`PHOTO_LIBRARY = "~/Pictures"`
+: Absolute path to the folder where the original photos are kept, organized in sub-folders.
+
+`PHOTO_GALLERY = (1024, 768, 80)`
+: For photos in galleries, maximum width and height, plus JPEG quality as a percentage. This would typically be the size of the photo displayed when the reader clicks a thumbnail.
+
+`PHOTO_ARTICLE = (760, 506, 80)`
+: For photos associated with articles, maximum width, height, and quality. The maximum size would typically depend on the needs of the theme. 760px is suitable for the theme `notmyidea`.
+
+`PHOTO_THUMB = (192, 144, 60)`
+: For thumbnails, maximum width, height, and quality.
+
+`PHOTO_SQUARE_THUMB = False`
+: Crops thumbnails to make them square.
+
+`PHOTO_RESIZE_JOBS = 5`
+: Number of parallel resize jobs to be run. Defaults to 1.
+
+`PHOTO_WATERMARK = True`
+: Adds a watermark to all photos in articles and pages. Defaults to using your site name.
+
+`PHOTO_WATERMARK_TEXT' = SITENAME`
+: Allow the user to change the watermark text or remove it completely. By default it uses [SourceCodePro-Bold](http://www.adobe.com/products/type/font-information/source-code-pro-readme.html) as the font.
+
+`PHOTO_WATERMARK_IMG = ''`
+: Allows the user to add an image in addition to or as the only watermark. Set the variable to the location.
+
+**The following features require the piexif library**
+`PHOTO_EXIF_KEEP = True`
+: Keeps the exif of the input photo.
+
+`PHOTO_EXIF_REMOVE_GPS = True`
+: Removes any GPS information from the files exif data.
+
+`PHOTO_EXIF_COPYRIGHT = 'COPYRIGHT'`
+: Attaches an author and a license to the file. Choices include:
+ - `COPYRIGHT`: Copyright
+ - `CC0`: Public Domain
+ - `CC-BY-NC-ND`: Creative Commons Attribution-NonCommercial-NoDerivatives
+ - `CC-BY-NC-SA`: Creative Commons Attribution-NonCommercial-ShareAlike
+ - `CC-BY`: Creative Commons Attribution
+ - `CC-BY-SA`: Creative Commons Attribution-ShareAlike
+ - `CC-BY-NC`: Creative Commons Attribution-NonCommercial
+ - `CC-BY-ND`: Creative Commons Attribution-NoDerivatives
+
+`PHOTO_EXIF_COPYRIGHT_AUTHOR = 'Your Name Here'`
+: Adds an author name to the photo's exif and copyright statement. Defaults to `AUTHOR` value from the `pelicanconf.py`
+
+The plug-in automatically resizes the photos and publishes them to the following output folder:
+
+ ./output/photos
+
+**WARNING:** The plug-in can take hours to resize 40,000 photos, therefore, photos and thumbnails are only generated once. Clean the output folders to regenerate the resized photos again.
+
+## How to use
+
+Maintain an organized library of high resolution photos somewhere on disk, using folders to group related images. The default path `~/Pictures` is convenient for Mac OS X users.
+
+* To create a gallery of photos, add the metadata field `gallery: {photo}folder` to an article. To simplify the transition from the plug-in Gallery, the syntax `gallery: {filename}folder` is also accepted.
+* You can now have multiple galleries. The galleries need to be seperated by a comma in the metadata field. The syntax is gallery: `{photo}folder, {photo}folder2`. You can also add titles to your galleries. The syntax is: `{photo}folder, {photo}folder2{This is a title}`. Using the following example the first gallery would have the title of the folder location and the second would have the title `This is a tile.`
+* To use an image in the body of the text, just use the syntax `{photo}folder/image.jpg` instead of the usual `{filename}/images/image.jpg`.
+* To use an image in the body of the text, which can be used with [Lightbox](http://lokeshdhakar.com/projects/lightbox2/) just use the syntax `{lightbox}folder/image.jpg`. For use with other implementations, the gallery and caption attribute names can be set with `PHOTO_LIGHTBOX_GALLERY_ATTR` and `PHOTO_LIGHTBOX_CAPTION_ATTR`.
+* To associate an image with an article, add the metadata field `image: {photo}folder/image.jpg` to an article. Use associated images to improve navigation. For compatibility, the syntax `image: {filename}/images/image.jpg` is also accepted.
+
+### Exif, Captions, and Blacklists
+Folders of photos may optionally have three text files, where each line describes one photo. You can use the `#` to comment out a line. Generating these optional files is left as an exercise for the reader (but consider using Phil Harvey's [exiftool](http://www.sno.phy.queensu.ca/~phil/exiftool/)). See below for one method of extracting exif data.
+
+`exif.txt`
+: Associates compact technical information with photos, typically the camera settings. For example:
+
+ best.jpg: Canon EOS 5D Mark II - 20mm f/8 1/250s ISO 100
+ night.jpg: Canon EOS 5D Mark II - 47mm f/8 5s ISO 100
+ # new.jpg: Canon EOS 5D Mark II - 47mm f/8 5s ISO 100
+
+`captions.txt`
+: Associates comments with photos. For example:
+
+ best.jpg: My best photo ever! How lucky of me!
+ night.jpg: Twilight over the dam.
+ # new.jpg: My new photo blog entry is not quite ready.
+
+`blacklist.txt`
+: Skips photos the user does not want to include. For example:
+
+ this-file-will-be-skipped.jpg
+ this-one-will-be-skipped-too.jpg
+ # but-this-file-will-NOT-be-skipped.jpg
+ this-one-will-be-also-skipped.jpg
+
+
+Here is an example Markdown article that shows the four use cases:
+
+ title: My Article
+ gallery: {photo}favorite
+ image: {photo}favorite/best.jpg
+
+ Here are my best photos, taken with my favorite camera:
+ ![]({photo}mybag/camera.jpg).
+ ![]({lightbox}mybag/flash.jpg).
+
+The default behavior of the Photos plugin removes the exif information from the file. If you would like to keep the exif information, you can install the `piexif` library for python and add the following settings to keep some or all of the exif information. This feature is not a replacement for the `exif.txt` feature but in addition to that feature. This feature currently only works with jpeg input files.
+
+## How to change the Jinja templates
+
+The plugin provides the following variables to your templates:
+
+`article.photo_image`
+: For articles with an associated photo, a tuple with the following information:
+
+* The filename of the original photo.
+* The output path to the generated photo.
+* The output path to the generated thumbnail.
+
+For example, modify the template `article.html` as shown below to display the associated image before the article content:
+
+```html
+<div class="entry-content">
+ {% if article.photo_image %}<img src="{{ SITEURL }}/{{ article.photo_image[1] }}" />{% endif %}
+ {% include 'article_infos.html' %}
+ {{ article.content }}
+</div><!-- /.entry-content -->
+```
+
+`article.photo_gallery`
+: For articles with a gallery, a list of the photos in the gallery. Each item in the list is a tuple with five elements:
+
+* The title of the gallery
+* The filename of the original photo.
+* The output path to the generated photo.
+* The output path to the generated thumbnail.
+* The EXIF information of the photo, as read from the file `exif.txt`.
+* The caption of the photo, as read from `captions.txt`.
+
+For example, add the following to the template `article.html` to add the gallery as the end of the article:
+
+```html
+{% if article.photo_gallery %}
+<div class="gallery">
+ {% for title, gallery in article.photo_gallery %}
+ <h1>{{ title }}</h1>
+ {% for name, photo, thumb, exif, caption in gallery %}
+ <a href="{{ SITEURL }}/{{ photo }}" title="{{ name }}" exif="{{ exif }}" caption="{{ caption }}"><img src="{{ SITEURL }}/{{ thumb }}"></a>
+ {% endfor %}
+ {% endfor %}
+</div>
+{% endif %}
+```
+
+For example, add the following to the template `index.html`, inside the `entry-content`, to display the thumbnail with a link to the article:
+
+```html
+{% if article.photo_image %}<a href="{{ SITEURL }}/{{ article.url }}"><img src="{{ SITEURL }}/{{ article.photo_image[2] }}"
+ style="display: inline; float: right; margin: 2px 0 2ex 4ex;" /></a>
+{% endif %}
+```
+
+## How to make the gallery lightbox
+
+There are several JavaScript libraries that display a list of images as a lightbox. The example below uses [Magnific Popup](http://dimsemenov.com/plugins/magnific-popup/), which allows the more complex initialization needed to display both the filename, the compact technical information, and the caption. The solution would be simpler if photos did not show any extra information.
+
+Copy the files `magnific-popup.css` and `magnific-popup.js` to the root of your Pelican template.
+
+Add the following to the template `base.html`, inside the HTML `head` tags:
+
+```html
+{% if (article and article.photo_gallery) or (articles_page and articles_page.object_list[0].photo_gallery) %}
+ <link rel="stylesheet" href="{{ SITEURL }}/{{ THEME_STATIC_DIR }}/magnific-popup.css">
+{% endif %}
+```
+
+Add the following to the template `base.html`, before the closing HTML `</body>` tag:
+
+```JavaScript
+{% if (article and article.photo_gallery) or (articles_page and articles_page.object_list[0].photo_gallery) %}
+<!-- jQuery 1.7.2+ or Zepto.js 1.0+ -->
+<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
+
+<!-- Magnific Popup core JS file -->
+<script src="{{ SITEURL }}/{{ THEME_STATIC_DIR }}/magnific-popup.js"></script>
+<script>
+$('.gallery').magnificPopup({
+ delegate: 'a',
+ type: 'image',
+ gallery: {
+ enabled: true,
+ navigateByImgClick: true,
+ preload: [1,2]
+ },
+ image: {
+ titleSrc: function(item) {
+ if (item.el.attr('caption') && item.el.attr('exif')) {
+ return (item.el.attr('caption').replace(/\\n/g, '<br />') +
+ '<small>' + item.el.attr('title') + ' - ' + item.el.attr('exif') + '</small>');
+ }
+ return item.el.attr('title') + '<small>' + item.el.attr('exif') + '</small>';
+ } }
+});
+</script>
+{% endif %}
+```
+
+## How to make a Bootstrap Carousel
+
+If you are using bootstrap, the following code is an example of how one could create a carousel.
+
+```html
+{% if article.photo_gallery %}
+ {% for title, gallery in article.photo_gallery %}
+ <h1>{{ title }}</h1>
+ <div id="carousel-{{ loop.index }}" class="carousel slide">
+ <ol class="carousel-indicators">
+ {% for i in range(0, gallery|length) %}
+ <li data-target="#carousel-{{ loop.index }}" data-slide-to="{{ i }}" {% if i==0 %} class="active" {% endif %}></li>
+ {% endfor %}
+ </ol>
+ <div class="carousel-inner">
+ {% for name, photo, thumb, exif, caption in gallery %}
+ {% if loop.first %}
+ <div class="item active">
+ {% else %}
+ <div class="item">
+ {% endif %}
+ <img src="{{ SITEURL }}/{{ photo }}" exif="{{ exif }}" alt="{{ caption }}">
+ <div class="carousel-caption">
+ <h5>{{ caption }}</h5>
+ </div> <!-- carousel-caption -->
+ </div> <!-- item -->
+ {% endfor %}
+ </div> <!-- carousel-inner -->
+ <a class="left carousel-control" href="#carousel-{{ loop.index }}" data-slide="prev">
+ <span class="glyphicon glyphicon-chevron-left"></span>
+ </a>
+ <a class="right carousel-control" href="#carousel-{{ loop.index }}" data-slide="next">
+ <span class="glyphicon glyphicon-chevron-right"></span>
+ </a>
+ </div> <!-- closes carousel-{{ loop.index }} -->
+ {% endfor %}
+{% endif %}
+```
+
+## Exiftool example
+
+You can add the following stanza to your fab file if you are using `fabric` to generate the appropriate text files for your galleries. You need to set the location of `Exiftool` control files.
+
+```Python
+def photo_gallery_gen(location):
+ """Create gallery metadata files."""
+ local_path = os.getcwd() + 'LOCATION OF YOUR EXIF CONTROL FILES'
+ with lcd(location):
+ local("exiftool -p {fmt_path}/exif.fmt . > exif.txt".format(
+ fmt_path=local_path))
+ local("exiftool -p {fmt_path}/captions.fmt . > captions.txt".format(
+ fmt_path=local_path))
+
+```
+
+`captions.fmt` example file
+
+```
+$FileName: $Description
+```
+
+`exif.fmt` example file
+
+```
+$FileName: $CreateDate - $Make $Model Stats:(f/$Aperture, ${ShutterSpeed}s, ISO $ISO Flash: $Flash) GPS:($GPSPosition $GPSAltitude)
+```
+
+## Known use cases
+
+[pxquim.pt](http://pxquim.pt/) uses Photos and the plug-in Sub-parts to publish 600 photo galleries with 40,000 photos. Photos keeps the high-resolution photos separate from the site articles.
+
+[pxquim.com](http://pxquim.com/) uses sub-parts to cover conferences, where it makes sense to have a sub-part for each speaker.
+
+## Alternatives
+
+Gallery
+: Galleries are distinct entities, without the organizational capabilities of articles. Photos must be resized separately, and must be kept with the source of the blog. Gallery was the initial inspiration for Photos.
+
+Image_process
+: Resize and process images in the article body in a more flexible way (based on the CSS class of the image), but without the ability to create galleries. The source photos must be kept with the source of the blog.
diff --git a/theme/plugins/photos/SourceCodePro-Bold.otf b/theme/plugins/photos/SourceCodePro-Bold.otf
new file mode 100644
index 0000000..f4e576c
--- /dev/null
+++ b/theme/plugins/photos/SourceCodePro-Bold.otf
Binary files differ
diff --git a/theme/plugins/photos/SourceCodePro-Regular.otf b/theme/plugins/photos/SourceCodePro-Regular.otf
new file mode 100644
index 0000000..4e3b9d0
--- /dev/null
+++ b/theme/plugins/photos/SourceCodePro-Regular.otf
Binary files differ
diff --git a/theme/plugins/photos/__init__.py b/theme/plugins/photos/__init__.py
new file mode 100644
index 0000000..9832420
--- /dev/null
+++ b/theme/plugins/photos/__init__.py
@@ -0,0 +1 @@
+from .photos import *
diff --git a/theme/plugins/photos/licenses.json b/theme/plugins/photos/licenses.json
new file mode 100644
index 0000000..c727e21
--- /dev/null
+++ b/theme/plugins/photos/licenses.json
@@ -0,0 +1,30 @@
+{
+ "CC-BY-NC-ND": {
+ "URL": "https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode",
+ "Text": "Copyleft license, {Author} {Year}. Content licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License, except where indicated otherwise. See {URL} for more information."
+ },
+ "CC-BY-NC-SA": {
+ "URL": "https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode",
+ "Text": "Copyleft license, {Author} {Year}. Content licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where indicated otherwise. See {URL} for more information."
+ },
+ "CC-BY": {
+ "URL": "https://creativecommons.org/licenses/by/4.0/legalcode",
+ "Text": "Copyleft license, {Author} {Year}. Content licensed under a Creative Commons Attribution 4.0 International License, except where indicated otherwise. See {URL} for more information."
+ },
+ "CC-BY-SA": {
+ "URL": "https://creativecommons.org/licenses/by-sa/4.0/legalcode",
+ "Text": "Copyleft license, {Author} {Year}. Content licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, except where indicated otherwise. See {URL} for more information."
+ },
+ "CC0": {
+ "URL": "https://creativecommons.org/publicdomain/zero/1.0/",
+ "Text": "CC0 Copyleft license, {Author} {Year}. To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. See {URL} for more information."
+ },
+ "CC-BY-NC": {
+ "URL": "https://creativecommons.org/licenses/by-nc/4.0/legalcode",
+ "Text": "Copyleft license, {Author} {Year}. Content licensed under a Creative Commons Attribution-NonCommercial 4.0 International License, except where indicated otherwise. See {URL} for more information."
+ },
+ "CC-BY-ND": {
+ "URL": "https://creativecommons.org/licenses/by-nd/4.0/legalcode",
+ "Text": "Copyleft license, {Author} {Year}. Content licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License, except where indicated otherwise. See {URL} for more information."
+ }
+}
diff --git a/theme/plugins/photos/photos.py b/theme/plugins/photos/photos.py
new file mode 100644
index 0000000..d0c1af1
--- /dev/null
+++ b/theme/plugins/photos/photos.py
@@ -0,0 +1,589 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+import datetime
+import itertools
+import json
+import logging
+import multiprocessing
+import os
+import pprint
+import re
+import sys
+
+from pelican.generators import ArticlesGenerator
+from pelican.generators import PagesGenerator
+from pelican.settings import DEFAULT_CONFIG
+from pelican import signals
+from pelican.utils import pelican_open
+
+logger = logging.getLogger(__name__)
+
+try:
+ from PIL import Image
+ from PIL import ImageDraw
+ from PIL import ImageEnhance
+ from PIL import ImageFont
+ from PIL import ImageOps
+except ImportError:
+ logger.error('PIL/Pillow not found')
+
+try:
+ import piexif
+except ImportError:
+ ispiexif = False
+ logger.warning('piexif not found! Cannot use exif manipulation features')
+else:
+ ispiexif = True
+ logger.debug('piexif found.')
+
+
+def initialized(pelican):
+ p = os.path.expanduser('~/Pictures')
+
+ DEFAULT_CONFIG.setdefault('PHOTO_LIBRARY', p)
+ DEFAULT_CONFIG.setdefault('PHOTO_GALLERY', (1024, 768, 80))
+ DEFAULT_CONFIG.setdefault('PHOTO_ARTICLE', (760, 506, 80))
+ DEFAULT_CONFIG.setdefault('PHOTO_THUMB', (192, 144, 60))
+ DEFAULT_CONFIG.setdefault('PHOTO_SQUARE_THUMB', False)
+ DEFAULT_CONFIG.setdefault('PHOTO_GALLERY_TITLE', '')
+ DEFAULT_CONFIG.setdefault('PHOTO_ALPHA_BACKGROUND_COLOR', (255, 255, 255))
+ DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK', False)
+ DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_THUMB', False)
+ DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_TEXT', DEFAULT_CONFIG['SITENAME'])
+ DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_TEXT_COLOR', (255, 255, 255))
+ DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_IMG', '')
+ DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_IMG_SIZE', False)
+ DEFAULT_CONFIG.setdefault('PHOTO_RESIZE_JOBS', 1)
+ DEFAULT_CONFIG.setdefault('PHOTO_EXIF_KEEP', False)
+ DEFAULT_CONFIG.setdefault('PHOTO_EXIF_REMOVE_GPS', False)
+ DEFAULT_CONFIG.setdefault('PHOTO_EXIF_AUTOROTATE', True)
+ DEFAULT_CONFIG.setdefault('PHOTO_EXIF_COPYRIGHT', False)
+ DEFAULT_CONFIG.setdefault('PHOTO_EXIF_COPYRIGHT_AUTHOR', DEFAULT_CONFIG['SITENAME'])
+ DEFAULT_CONFIG.setdefault('PHOTO_LIGHTBOX_GALLERY_ATTR', 'data-lightbox')
+ DEFAULT_CONFIG.setdefault('PHOTO_LIGHTBOX_CAPTION_ATTR', 'data-title')
+
+ DEFAULT_CONFIG['queue_resize'] = {}
+ DEFAULT_CONFIG['created_galleries'] = {}
+ DEFAULT_CONFIG['plugin_dir'] = os.path.dirname(os.path.realpath(__file__))
+
+ if pelican:
+ pelican.settings.setdefault('PHOTO_LIBRARY', p)
+ pelican.settings.setdefault('PHOTO_GALLERY', (1024, 768, 80))
+ pelican.settings.setdefault('PHOTO_ARTICLE', (760, 506, 80))
+ pelican.settings.setdefault('PHOTO_THUMB', (192, 144, 60))
+ pelican.settings.setdefault('PHOTO_SQUARE_THUMB', False)
+ pelican.settings.setdefault('PHOTO_GALLERY_TITLE', '')
+ pelican.settings.setdefault('PHOTO_ALPHA_BACKGROUND_COLOR', (255, 255, 255))
+ pelican.settings.setdefault('PHOTO_WATERMARK', False)
+ pelican.settings.setdefault('PHOTO_WATERMARK_THUMB', False)
+ pelican.settings.setdefault('PHOTO_WATERMARK_TEXT', pelican.settings['SITENAME'])
+ pelican.settings.setdefault('PHOTO_WATERMARK_TEXT_COLOR', (255, 255, 255))
+ pelican.settings.setdefault('PHOTO_WATERMARK_IMG', '')
+ pelican.settings.setdefault('PHOTO_WATERMARK_IMG_SIZE', False)
+ pelican.settings.setdefault('PHOTO_RESIZE_JOBS', 1)
+ pelican.settings.setdefault('PHOTO_EXIF_KEEP', False)
+ pelican.settings.setdefault('PHOTO_EXIF_REMOVE_GPS', False)
+ pelican.settings.setdefault('PHOTO_EXIF_AUTOROTATE', True)
+ pelican.settings.setdefault('PHOTO_EXIF_COPYRIGHT', False)
+ pelican.settings.setdefault('PHOTO_EXIF_COPYRIGHT_AUTHOR', pelican.settings['AUTHOR'])
+ pelican.settings.setdefault('PHOTO_LIGHTBOX_GALLERY_ATTR', 'data-lightbox')
+ pelican.settings.setdefault('PHOTO_LIGHTBOX_CAPTION_ATTR', 'data-title')
+
+
+def read_notes(filename, msg=None):
+ notes = {}
+ try:
+ with pelican_open(filename) as text:
+ for line in text.splitlines():
+ if line.startswith('#'):
+ continue
+ m = line.split(':', 1)
+ if len(m) > 1:
+ pic = m[0].strip()
+ note = m[1].strip()
+ if pic and note:
+ notes[pic] = note
+ else:
+ notes[line] = ''
+ except Exception as e:
+ if msg:
+ logger.info('{} at file {}'.format(msg, filename))
+ logger.debug('read_notes issue: {} at file {}. Debug message:{}'.format(msg, filename, e))
+ return notes
+
+
+def enqueue_resize(orig, resized, spec=(640, 480, 80)):
+ if resized not in DEFAULT_CONFIG['queue_resize']:
+ DEFAULT_CONFIG['queue_resize'][resized] = (orig, spec)
+ elif DEFAULT_CONFIG['queue_resize'][resized] != (orig, spec):
+ logger.error('photos: resize conflict for {}, {}-{} is not {}-{}'.format(resized, DEFAULT_CONFIG['queue_resize'][resized][0], DEFAULT_CONFIG['queue_resize'][resized][1], orig, spec))
+
+
+def isalpha(img):
+ return True if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info) else False
+
+
+def remove_alpha(img, bg_color):
+ background = Image.new("RGB", img.size, bg_color)
+ background.paste(img, mask=img.split()[3]) # 3 is the alpha channel
+ return background
+
+
+def ReduceOpacity(im, opacity):
+ """Reduces Opacity.
+
+ Returns an image with reduced opacity.
+ Taken from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
+ """
+ assert opacity >= 0 and opacity <= 1
+ if isalpha(im):
+ im = im.copy()
+ else:
+ im = im.convert('RGBA')
+
+ alpha = im.split()[3]
+ alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
+ im.putalpha(alpha)
+ return im
+
+
+def watermark_photo(image, settings):
+ margin = [10, 10]
+ opacity = 0.6
+
+ watermark_layer = Image.new("RGBA", image.size, (0, 0, 0, 0))
+ draw_watermark = ImageDraw.Draw(watermark_layer)
+ text_reducer = 32
+ image_reducer = 8
+ text_size = [0, 0]
+ mark_size = [0, 0]
+ text_position = [0, 0]
+
+ if settings['PHOTO_WATERMARK_TEXT']:
+ font_name = 'SourceCodePro-Bold.otf'
+ default_font = os.path.join(DEFAULT_CONFIG['plugin_dir'], font_name)
+ font = ImageFont.FreeTypeFont(default_font, watermark_layer.size[0] // text_reducer)
+ text_size = draw_watermark.textsize(settings['PHOTO_WATERMARK_TEXT'], font)
+ text_position = [image.size[i] - text_size[i] - margin[i] for i in [0, 1]]
+ draw_watermark.text(text_position, settings['PHOTO_WATERMARK_TEXT'], settings['PHOTO_WATERMARK_TEXT_COLOR'], font=font)
+
+ if settings['PHOTO_WATERMARK_IMG']:
+ mark_image = Image.open(settings['PHOTO_WATERMARK_IMG'])
+ mark_image_size = [watermark_layer.size[0] // image_reducer for size in mark_size]
+ mark_image_size = settings['PHOTO_WATERMARK_IMG_SIZE'] if settings['PHOTO_WATERMARK_IMG_SIZE'] else mark_image_size
+ mark_image.thumbnail(mark_image_size, Image.ANTIALIAS)
+ mark_position = [watermark_layer.size[i] - mark_image.size[i] - margin[i] for i in [0, 1]]
+ mark_position = tuple([mark_position[0] - (text_size[0] // 2) + (mark_image_size[0] // 2), mark_position[1] - text_size[1]])
+
+ if not isalpha(mark_image):
+ mark_image = mark_image.convert('RGBA')
+
+ watermark_layer.paste(mark_image, mark_position, mark_image)
+
+ watermark_layer = ReduceOpacity(watermark_layer, opacity)
+ image.paste(watermark_layer, (0, 0), watermark_layer)
+
+ return image
+
+
+def rotate_image(img, exif_dict):
+ if "exif" in img.info and piexif.ImageIFD.Orientation in exif_dict["0th"]:
+ orientation = exif_dict["0th"].pop(piexif.ImageIFD.Orientation)
+ if orientation == 2:
+ img = img.transpose(Image.FLIP_LEFT_RIGHT)
+ elif orientation == 3:
+ img = img.rotate(180)
+ elif orientation == 4:
+ img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT)
+ elif orientation == 5:
+ img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
+ elif orientation == 6:
+ img = img.rotate(-90, expand=True)
+ elif orientation == 7:
+ img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
+ elif orientation == 8:
+ img = img.rotate(90)
+
+ return (img, exif_dict)
+
+
+def build_license(license, author):
+ year = datetime.datetime.now().year
+ license_file = os.path.join(DEFAULT_CONFIG['plugin_dir'], 'licenses.json')
+
+ with open(license_file) as data_file:
+ licenses = json.load(data_file)
+
+ if any(license in k for k in licenses):
+ return licenses[license]['Text'].format(Author=author, Year=year, URL=licenses[license]['URL'])
+ else:
+ return 'Copyright {Year} {Author}, All Rights Reserved'.format(Author=author, Year=year)
+
+
+def manipulate_exif(img, settings):
+ try:
+ exif = piexif.load(img.info['exif'])
+ except Exception:
+ logger.debug('EXIF information not found')
+ exif = {}
+
+ if settings['PHOTO_EXIF_AUTOROTATE']:
+ img, exif = rotate_image(img, exif)
+
+ if settings['PHOTO_EXIF_REMOVE_GPS']:
+ exif.pop('GPS')
+
+ if settings['PHOTO_EXIF_COPYRIGHT']:
+
+ # We want to be minimally destructive to any preset exif author or copyright information.
+ # If there is copyright or author information prefer that over everything else.
+ if not exif['0th'].get(piexif.ImageIFD.Artist):
+ exif['0th'][piexif.ImageIFD.Artist] = settings['PHOTO_EXIF_COPYRIGHT_AUTHOR']
+ author = settings['PHOTO_EXIF_COPYRIGHT_AUTHOR']
+
+ if not exif['0th'].get(piexif.ImageIFD.Copyright):
+ license = build_license(settings['PHOTO_EXIF_COPYRIGHT'], author)
+ exif['0th'][piexif.ImageIFD.Copyright] = license
+
+ return (img, piexif.dump(exif))
+
+
+def resize_worker(orig, resized, spec, settings):
+ logger.info('photos: make photo {} -> {}'.format(orig, resized))
+ im = Image.open(orig)
+
+ if ispiexif and settings['PHOTO_EXIF_KEEP'] and im.format == 'JPEG': # Only works with JPEG exif for sure.
+ try:
+ im, exif_copy = manipulate_exif(im, settings)
+ except:
+ logger.info('photos: no EXIF or EXIF error in {}'.format(orig))
+ exif_copy = b''
+ else:
+ exif_copy = b''
+
+ icc_profile = im.info.get("icc_profile", None)
+
+ if settings['PHOTO_SQUARE_THUMB'] and spec == settings['PHOTO_THUMB']:
+ im = ImageOps.fit(im, (spec[0], spec[1]), Image.ANTIALIAS)
+
+ im.thumbnail((spec[0], spec[1]), Image.ANTIALIAS)
+ directory = os.path.split(resized)[0]
+
+ if isalpha(im):
+ im = remove_alpha(im, settings['PHOTO_ALPHA_BACKGROUND_COLOR'])
+
+ if not os.path.exists(directory):
+ try:
+ os.makedirs(directory)
+ except Exception:
+ logger.exception('Could not create {}'.format(directory))
+ else:
+ logger.debug('Directory already exists at {}'.format(os.path.split(resized)[0]))
+
+ if settings['PHOTO_WATERMARK']:
+ isthumb = True if spec == settings['PHOTO_THUMB'] else False
+ if not isthumb or (isthumb and settings['PHOTO_WATERMARK_THUMB']):
+ im = watermark_photo(im, settings)
+
+ im.save(resized, 'JPEG', quality=spec[2], icc_profile=icc_profile, exif=exif_copy)
+
+
+def resize_photos(generator, writer):
+ if generator.settings['PHOTO_RESIZE_JOBS'] == -1:
+ debug = True
+ generator.settings['PHOTO_RESIZE_JOBS'] = 1
+ else:
+ debug = False
+
+ pool = multiprocessing.Pool(generator.settings['PHOTO_RESIZE_JOBS'])
+ logger.debug('Debug Status: {}'.format(debug))
+ for resized, what in DEFAULT_CONFIG['queue_resize'].items():
+ resized = os.path.join(generator.output_path, resized)
+ orig, spec = what
+ if (not os.path.isfile(resized) or os.path.getmtime(orig) > os.path.getmtime(resized)):
+ if debug:
+ resize_worker(orig, resized, spec, generator.settings)
+ else:
+ pool.apply_async(resize_worker, (orig, resized, spec, generator.settings))
+
+ pool.close()
+ pool.join()
+
+
+def detect_content(content):
+ hrefs = None
+
+ def replacer(m):
+ what = m.group('what')
+ value = m.group('value')
+ tag = m.group('tag')
+ output = m.group(0)
+
+ if what in ('photo', 'lightbox'):
+ if value.startswith('/'):
+ value = value[1:]
+
+ path = os.path.join(
+ os.path.expanduser(settings['PHOTO_LIBRARY']),
+ value
+ )
+
+ if os.path.isfile(path):
+ photo_prefix = os.path.splitext(value)[0].lower()
+
+ if what == 'photo':
+ photo_article = photo_prefix + 'a.jpg'
+ enqueue_resize(
+ path,
+ os.path.join('photos', photo_article),
+ settings['PHOTO_ARTICLE']
+ )
+
+ output = ''.join((
+ '<',
+ m.group('tag'),
+ m.group('attrs_before'),
+ m.group('src'),
+ '=',
+ m.group('quote'),
+ os.path.join(settings['SITEURL'], 'photos', photo_article),
+ m.group('quote'),
+ m.group('attrs_after'),
+ ))
+
+ elif what == 'lightbox' and tag == 'img':
+ photo_gallery = photo_prefix + '.jpg'
+ enqueue_resize(
+ path,
+ os.path.join('photos', photo_gallery),
+ settings['PHOTO_GALLERY']
+ )
+
+ photo_thumb = photo_prefix + 't.jpg'
+ enqueue_resize(
+ path,
+ os.path.join('photos', photo_thumb),
+ settings['PHOTO_THUMB']
+ )
+
+ lightbox_attr_list = ['']
+
+ gallery_name = value.split('/')[0]
+ lightbox_attr_list.append('{}="{}"'.format(
+ settings['PHOTO_LIGHTBOX_GALLERY_ATTR'],
+ gallery_name
+ ))
+
+ captions = read_notes(
+ os.path.join(os.path.dirname(path), 'captions.txt'),
+ msg = 'photos: No captions for gallery'
+ )
+ caption = captions.get(os.path.basename(path)) if captions else None
+ if caption:
+ lightbox_attr_list.append('{}="{}"'.format(
+ settings['PHOTO_LIGHTBOX_CAPTION_ATTR'],
+ caption
+ ))
+
+ lightbox_attrs = ' '.join(lightbox_attr_list)
+
+ output = ''.join((
+ '<a href=',
+ m.group('quote'),
+ os.path.join(settings['SITEURL'], 'photos', photo_gallery),
+ m.group('quote'),
+ lightbox_attrs,
+ '><img',
+ m.group('attrs_before'),
+ 'src=',
+ m.group('quote'),
+ os.path.join(settings['SITEURL'], 'photos', photo_thumb),
+ m.group('quote'),
+ m.group('attrs_after'),
+ '</a>'
+ ))
+
+ else:
+ logger.error('photos: No photo %s', path)
+
+ return output
+
+ if hrefs is None:
+ regex = r"""
+ <\s*
+ (?P<tag>[^\s\>]+) # detect the tag
+ (?P<attrs_before>[^\>]*)
+ (?P<src>href|src) # match tag with src and href attr
+ \s*=
+ (?P<quote>["\']) # require value to be quoted
+ (?P<path>{0}(?P<value>.*?)) # the url value
+ (?P=quote)
+ (?P<attrs_after>[^\>]*>)
+ """.format(
+ content.settings['INTRASITE_LINK_REGEX']
+ )
+ hrefs = re.compile(regex, re.X)
+
+ if content._content and ('{photo}' in content._content or '{lightbox}' in content._content):
+ settings = content.settings
+ content._content = hrefs.sub(replacer, content._content)
+
+
+def galleries_string_decompose(gallery_string):
+ splitter_regex = re.compile(r'[\s,]*?({photo}|{filename})')
+ title_regex = re.compile(r'{(.+)}')
+ galleries = map(unicode.strip if sys.version_info.major == 2 else str.strip, filter(None, splitter_regex.split(gallery_string)))
+ galleries = [gallery[1:] if gallery.startswith('/') else gallery for gallery in galleries]
+ if len(galleries) % 2 == 0 and ' ' not in galleries:
+ galleries = zip(zip(['type'] * len(galleries[0::2]), galleries[0::2]), zip(['location'] * len(galleries[0::2]), galleries[1::2]))
+ galleries = [dict(gallery) for gallery in galleries]
+ for gallery in galleries:
+ title = re.search(title_regex, gallery['location'])
+ if title:
+ gallery['title'] = title.group(1)
+ gallery['location'] = re.sub(title_regex, '', gallery['location']).strip()
+ else:
+ gallery['title'] = DEFAULT_CONFIG['PHOTO_GALLERY_TITLE']
+ return galleries
+ else:
+ logger.error('Unexpected gallery location format! \n{}'.format(pprint.pformat(galleries)))
+
+
+def process_gallery(generator, content, location):
+ content.photo_gallery = []
+
+ galleries = galleries_string_decompose(location)
+
+ for gallery in galleries:
+
+ if gallery['location'] in DEFAULT_CONFIG['created_galleries']:
+ content.photo_gallery.append((gallery['location'], DEFAULT_CONFIG['created_galleries'][gallery]))
+ continue
+
+ if gallery['type'] == '{photo}':
+ dir_gallery = os.path.join(os.path.expanduser(generator.settings['PHOTO_LIBRARY']), gallery['location'])
+ rel_gallery = gallery['location']
+ elif gallery['type'] == '{filename}':
+ base_path = os.path.join(generator.path, content.relative_dir)
+ dir_gallery = os.path.join(base_path, gallery['location'])
+ rel_gallery = os.path.join(content.relative_dir, gallery['location'])
+
+ if os.path.isdir(dir_gallery):
+ logger.info('photos: Gallery detected: {}'.format(rel_gallery))
+ dir_photo = os.path.join('photos', rel_gallery.lower())
+ dir_thumb = os.path.join('photos', rel_gallery.lower())
+ exifs = read_notes(os.path.join(dir_gallery, 'exif.txt'),
+ msg='photos: No EXIF for gallery')
+ captions = read_notes(os.path.join(dir_gallery, 'captions.txt'), msg='photos: No captions for gallery')
+ blacklist = read_notes(os.path.join(dir_gallery, 'blacklist.txt'), msg='photos: No blacklist for gallery')
+ content_gallery = []
+
+ title = gallery['title']
+ for pic in sorted(os.listdir(dir_gallery)):
+ if pic.startswith('.'):
+ continue
+ if pic.endswith('.txt'):
+ continue
+ if pic in blacklist:
+ continue
+ photo = os.path.splitext(pic)[0].lower() + '.jpg'
+ thumb = os.path.splitext(pic)[0].lower() + 't.jpg'
+ content_gallery.append((
+ pic,
+ os.path.join(dir_photo, photo),
+ os.path.join(dir_thumb, thumb),
+ exifs.get(pic, ''),
+ captions.get(pic, '')))
+
+ enqueue_resize(
+ os.path.join(dir_gallery, pic),
+ os.path.join(dir_photo, photo),
+ generator.settings['PHOTO_GALLERY'])
+ enqueue_resize(
+ os.path.join(dir_gallery, pic),
+ os.path.join(dir_thumb, thumb),
+ generator.settings['PHOTO_THUMB'])
+
+ content.photo_gallery.append((title, content_gallery))
+ logger.debug('Gallery Data: '.format(pprint.pformat(content.photo_gallery)))
+ DEFAULT_CONFIG['created_galleries']['gallery'] = content_gallery
+ else:
+ logger.error('photos: Gallery does not exist: {} at {}'.format(gallery['location'], dir_gallery))
+
+
+def detect_gallery(generator, content):
+ if 'gallery' in content.metadata:
+ gallery = content.metadata.get('gallery')
+ if gallery.startswith('{photo}') or gallery.startswith('{filename}'):
+ process_gallery(generator, content, gallery)
+ elif gallery:
+ logger.error('photos: Gallery tag not recognized: {}'.format(gallery))
+
+
+def image_clipper(x):
+ return x[8:] if x[8] == '/' else x[7:]
+
+
+def file_clipper(x):
+ return x[11:] if x[10] == '/' else x[10:]
+
+
+def process_image(generator, content, image):
+ if image.startswith('{photo}'):
+ path = os.path.join(os.path.expanduser(generator.settings['PHOTO_LIBRARY']), image_clipper(image))
+ image = image_clipper(image)
+ elif image.startswith('{filename}'):
+ path = os.path.join(generator.path, content.relative_dir, file_clipper(image))
+ image = file_clipper(image)
+
+ if os.path.isfile(path):
+ photo = os.path.splitext(image)[0].lower() + 'a.jpg'
+ thumb = os.path.splitext(image)[0].lower() + 't.jpg'
+ content.photo_image = (
+ os.path.basename(image).lower(),
+ os.path.join('photos', photo),
+ os.path.join('photos', thumb))
+ enqueue_resize(
+ path,
+ os.path.join('photos', photo),
+ generator.settings['PHOTO_ARTICLE'])
+ enqueue_resize(
+ path,
+ os.path.join('photos', thumb),
+ generator.settings['PHOTO_THUMB'])
+ else:
+ logger.error('photo: No photo for {} at {}'.format(content.source_path, path))
+
+
+def detect_image(generator, content):
+ image = content.metadata.get('image', None)
+ if image:
+ if image.startswith('{photo}') or image.startswith('{filename}'):
+ process_image(generator, content, image)
+ else:
+ logger.error('photos: Image tag not recognized: {}'.format(image))
+
+
+def detect_images_and_galleries(generators):
+ """Runs generator on both pages and articles."""
+ for generator in generators:
+ if isinstance(generator, ArticlesGenerator):
+ for article in itertools.chain(generator.articles, generator.translations, generator.drafts):
+ detect_image(generator, article)
+ detect_gallery(generator, article)
+ elif isinstance(generator, PagesGenerator):
+ for page in itertools.chain(generator.pages, generator.translations, generator.hidden_pages):
+ detect_image(generator, page)
+ detect_gallery(generator, page)
+
+
+def register():
+ """Uses the new style of registration based on GitHub Pelican issue #314."""
+ signals.initialized.connect(initialized)
+ try:
+ signals.content_object_init.connect(detect_content)
+ signals.all_generators_finalized.connect(detect_images_and_galleries)
+ signals.article_writer_finalized.connect(resize_photos)
+ except Exception as e:
+ logger.exception('Plugin failed to execute: {}'.format(pprint.pformat(e)))