photos.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime
  4. import itertools
  5. import json
  6. import logging
  7. import multiprocessing
  8. import os
  9. import pprint
  10. import re
  11. import sys
  12. from pelican.generators import ArticlesGenerator
  13. from pelican.generators import PagesGenerator
  14. from pelican.settings import DEFAULT_CONFIG
  15. from pelican import signals
  16. from pelican.utils import pelican_open
  17. logger = logging.getLogger(__name__)
  18. try:
  19. from PIL import Image
  20. from PIL import ImageDraw
  21. from PIL import ImageEnhance
  22. from PIL import ImageFont
  23. except ImportError:
  24. logger.error('PIL/Pillow not found')
  25. try:
  26. import piexif
  27. except ImportError:
  28. ispiexif = False
  29. logger.warning('piexif not found! Cannot use exif manipulation features')
  30. else:
  31. ispiexif = True
  32. logger.debug('piexif found.')
  33. def initialized(pelican):
  34. p = os.path.expanduser('~/Pictures')
  35. DEFAULT_CONFIG.setdefault('PHOTO_LIBRARY', p)
  36. DEFAULT_CONFIG.setdefault('PHOTO_GALLERY', (1024, 768, 80))
  37. DEFAULT_CONFIG.setdefault('PHOTO_ARTICLE', (760, 506, 80))
  38. DEFAULT_CONFIG.setdefault('PHOTO_THUMB', (192, 144, 60))
  39. DEFAULT_CONFIG.setdefault('PHOTO_GALLERY_TITLE', '')
  40. DEFAULT_CONFIG.setdefault('PHOTO_ALPHA_BACKGROUND_COLOR', (255, 255, 255))
  41. DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK', False)
  42. DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_THUMB', False)
  43. DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_TEXT', DEFAULT_CONFIG['SITENAME'])
  44. DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_TEXT_COLOR', (255, 255, 255))
  45. DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_IMG', '')
  46. DEFAULT_CONFIG.setdefault('PHOTO_WATERMARK_IMG_SIZE', False)
  47. DEFAULT_CONFIG.setdefault('PHOTO_RESIZE_JOBS', 1)
  48. DEFAULT_CONFIG.setdefault('PHOTO_EXIF_KEEP', False)
  49. DEFAULT_CONFIG.setdefault('PHOTO_EXIF_REMOVE_GPS', False)
  50. DEFAULT_CONFIG.setdefault('PHOTO_EXIF_AUTOROTATE', True)
  51. DEFAULT_CONFIG.setdefault('PHOTO_EXIF_COPYRIGHT', False)
  52. DEFAULT_CONFIG.setdefault('PHOTO_EXIF_COPYRIGHT_AUTHOR', DEFAULT_CONFIG['SITENAME'])
  53. DEFAULT_CONFIG['queue_resize'] = {}
  54. DEFAULT_CONFIG['created_galleries'] = {}
  55. DEFAULT_CONFIG['plugin_dir'] = os.path.dirname(os.path.realpath(__file__))
  56. if pelican:
  57. pelican.settings.setdefault('PHOTO_LIBRARY', p)
  58. pelican.settings.setdefault('PHOTO_GALLERY', (1024, 768, 80))
  59. pelican.settings.setdefault('PHOTO_ARTICLE', (760, 506, 80))
  60. pelican.settings.setdefault('PHOTO_THUMB', (192, 144, 60))
  61. pelican.settings.setdefault('PHOTO_GALLERY_TITLE', '')
  62. pelican.settings.setdefault('PHOTO_ALPHA_BACKGROUND_COLOR', (255, 255, 255))
  63. pelican.settings.setdefault('PHOTO_WATERMARK', False)
  64. pelican.settings.setdefault('PHOTO_WATERMARK_THUMB', False)
  65. pelican.settings.setdefault('PHOTO_WATERMARK_TEXT', pelican.settings['SITENAME'])
  66. pelican.settings.setdefault('PHOTO_WATERMARK_TEXT_COLOR', (255, 255, 255))
  67. pelican.settings.setdefault('PHOTO_WATERMARK_IMG', '')
  68. pelican.settings.setdefault('PHOTO_WATERMARK_IMG_SIZE', False)
  69. pelican.settings.setdefault('PHOTO_RESIZE_JOBS', 1)
  70. pelican.settings.setdefault('PHOTO_EXIF_KEEP', False)
  71. pelican.settings.setdefault('PHOTO_EXIF_REMOVE_GPS', False)
  72. pelican.settings.setdefault('PHOTO_EXIF_AUTOROTATE', True)
  73. pelican.settings.setdefault('PHOTO_EXIF_COPYRIGHT', False)
  74. pelican.settings.setdefault('PHOTO_EXIF_COPYRIGHT_AUTHOR', pelican.settings['AUTHOR'])
  75. def read_notes(filename, msg=None):
  76. notes = {}
  77. try:
  78. with pelican_open(filename) as text:
  79. for line in text.splitlines():
  80. if line.startswith('#'):
  81. continue
  82. m = line.split(':', 1)
  83. if len(m) > 1:
  84. pic = m[0].strip()
  85. note = m[1].strip()
  86. if pic and note:
  87. notes[pic] = note
  88. else:
  89. notes[line] = ''
  90. except Exception as e:
  91. if msg:
  92. logger.warning('{} at file {}'.format(msg, filename))
  93. logger.debug('read_notes issue: {} at file {}. Debug message:{}'.format(msg, filename, e))
  94. return notes
  95. def enqueue_resize(orig, resized, spec=(640, 480, 80)):
  96. if resized not in DEFAULT_CONFIG['queue_resize']:
  97. DEFAULT_CONFIG['queue_resize'][resized] = (orig, spec)
  98. elif DEFAULT_CONFIG['queue_resize'][resized] != (orig, spec):
  99. logger.error('photos: resize conflict for {}, {}-{} is not {}-{}'.format(resized, DEFAULT_CONFIG['queue_resize'][resized][0], DEFAULT_CONFIG['queue_resize'][resized][1], orig, spec))
  100. def isalpha(img):
  101. return True if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info) else False
  102. def remove_alpha(img, bg_color):
  103. background = Image.new("RGB", img.size, bg_color)
  104. background.paste(img, mask=img.split()[3]) # 3 is the alpha channel
  105. return background
  106. def ReduceOpacity(im, opacity):
  107. """Reduces Opacity.
  108. Returns an image with reduced opacity.
  109. Taken from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
  110. """
  111. assert opacity >= 0 and opacity <= 1
  112. if isalpha(im):
  113. im = im.copy()
  114. else:
  115. im = im.convert('RGBA')
  116. alpha = im.split()[3]
  117. alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
  118. im.putalpha(alpha)
  119. return im
  120. def watermark_photo(image, settings):
  121. margin = [10, 10]
  122. opacity = 0.6
  123. watermark_layer = Image.new("RGBA", image.size, (0, 0, 0, 0))
  124. draw_watermark = ImageDraw.Draw(watermark_layer)
  125. text_reducer = 32
  126. image_reducer = 8
  127. text_size = [0, 0]
  128. mark_size = [0, 0]
  129. text_position = [0, 0]
  130. if settings['PHOTO_WATERMARK_TEXT']:
  131. font_name = 'SourceCodePro-Bold.otf'
  132. default_font = os.path.join(DEFAULT_CONFIG['plugin_dir'], font_name)
  133. font = ImageFont.FreeTypeFont(default_font, watermark_layer.size[0] // text_reducer)
  134. text_size = draw_watermark.textsize(settings['PHOTO_WATERMARK_TEXT'], font)
  135. text_position = [image.size[i] - text_size[i] - margin[i] for i in [0, 1]]
  136. draw_watermark.text(text_position, settings['PHOTO_WATERMARK_TEXT'], settings['PHOTO_WATERMARK_TEXT_COLOR'], font=font)
  137. if settings['PHOTO_WATERMARK_IMG']:
  138. mark_image = Image.open(settings['PHOTO_WATERMARK_IMG'])
  139. mark_image_size = [watermark_layer.size[0] // image_reducer for size in mark_size]
  140. mark_image_size = settings['PHOTO_WATERMARK_IMG_SIZE'] if settings['PHOTO_WATERMARK_IMG_SIZE'] else mark_image_size
  141. mark_image.thumbnail(mark_image_size, Image.ANTIALIAS)
  142. mark_position = [watermark_layer.size[i] - mark_image.size[i] - margin[i] for i in [0, 1]]
  143. mark_position = tuple([mark_position[0] - (text_size[0] // 2) + (mark_image_size[0] // 2), mark_position[1] - text_size[1]])
  144. if not isalpha(mark_image):
  145. mark_image = mark_image.convert('RGBA')
  146. watermark_layer.paste(mark_image, mark_position, mark_image)
  147. watermark_layer = ReduceOpacity(watermark_layer, opacity)
  148. image.paste(watermark_layer, (0, 0), watermark_layer)
  149. return image
  150. def rotate_image(img, exif_dict):
  151. if "exif" in img.info and piexif.ImageIFD.Orientation in exif_dict["0th"]:
  152. orientation = exif_dict["0th"].pop(piexif.ImageIFD.Orientation)
  153. if orientation == 2:
  154. img = img.transpose(Image.FLIP_LEFT_RIGHT)
  155. elif orientation == 3:
  156. img = img.rotate(180)
  157. elif orientation == 4:
  158. img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT)
  159. elif orientation == 5:
  160. img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
  161. elif orientation == 6:
  162. img = img.rotate(-90)
  163. elif orientation == 7:
  164. img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
  165. elif orientation == 8:
  166. img = img.rotate(90)
  167. return (img, exif_dict)
  168. def build_license(license, author):
  169. year = datetime.datetime.now().year
  170. license_file = os.path.join(DEFAULT_CONFIG['plugin_dir'], 'licenses.json')
  171. with open(license_file) as data_file:
  172. licenses = json.load(data_file)
  173. if any(license in k for k in licenses):
  174. return licenses[license]['Text'].format(Author=author, Year=year, URL=licenses[license]['URL'])
  175. else:
  176. return 'Copyright {Year} {Author}, All Rights Reserved'.format(Author=author, Year=year)
  177. def manipulate_exif(img, settings):
  178. try:
  179. exif = piexif.load(img.info['exif'])
  180. except Exception:
  181. logger.debug('EXIF information not found')
  182. exif = {}
  183. if settings['PHOTO_EXIF_AUTOROTATE']:
  184. img, exif = rotate_image(img, exif)
  185. if settings['PHOTO_EXIF_REMOVE_GPS']:
  186. exif.pop('GPS')
  187. if settings['PHOTO_EXIF_COPYRIGHT']:
  188. # We want to be minimally destructive to any preset exif author or copyright information.
  189. # If there is copyright or author information prefer that over everything else.
  190. if not exif['0th'].get(piexif.ImageIFD.Artist):
  191. exif['0th'][piexif.ImageIFD.Artist] = settings['PHOTO_EXIF_COPYRIGHT_AUTHOR']
  192. author = settings['PHOTO_EXIF_COPYRIGHT_AUTHOR']
  193. if not exif['0th'].get(piexif.ImageIFD.Copyright):
  194. license = build_license(settings['PHOTO_EXIF_COPYRIGHT'], author)
  195. exif['0th'][piexif.ImageIFD.Copyright] = license
  196. return (img, piexif.dump(exif))
  197. def resize_worker(orig, resized, spec, settings):
  198. logger.info('photos: make photo {} -> {}'.format(orig, resized))
  199. im = Image.open(orig)
  200. if ispiexif and settings['PHOTO_EXIF_KEEP'] and im.format == 'JPEG': # Only works with JPEG exif for sure.
  201. im, exif_copy = manipulate_exif(im, settings)
  202. else:
  203. exif_copy = b''
  204. icc_profile = im.info.get("icc_profile", None)
  205. im.thumbnail((spec[0], spec[1]), Image.ANTIALIAS)
  206. directory = os.path.split(resized)[0]
  207. if isalpha(im):
  208. im = remove_alpha(im, settings['PHOTO_ALPHA_BACKGROUND_COLOR'])
  209. if not os.path.exists(directory):
  210. try:
  211. os.makedirs(directory)
  212. except Exception:
  213. logger.exception('Could not create {}'.format(directory))
  214. else:
  215. logger.debug('Directory already exists at {}'.format(os.path.split(resized)[0]))
  216. if settings['PHOTO_WATERMARK']:
  217. isthumb = True if spec == settings['PHOTO_THUMB'] else False
  218. if not isthumb or (isthumb and settings['PHOTO_WATERMARK_THUMB']):
  219. im = watermark_photo(im, settings)
  220. im.save(resized, 'JPEG', quality=spec[2], icc_profile=icc_profile, exif=exif_copy)
  221. def resize_photos(generator, writer):
  222. if generator.settings['PHOTO_RESIZE_JOBS'] == -1:
  223. debug = True
  224. generator.settings['PHOTO_RESIZE_JOBS'] = 1
  225. else:
  226. debug = False
  227. pool = multiprocessing.Pool(generator.settings['PHOTO_RESIZE_JOBS'])
  228. logger.debug('Debug Status: {}'.format(debug))
  229. for resized, what in DEFAULT_CONFIG['queue_resize'].items():
  230. resized = os.path.join(generator.output_path, resized)
  231. orig, spec = what
  232. if (not os.path.isfile(resized) or os.path.getmtime(orig) > os.path.getmtime(resized)):
  233. if debug:
  234. resize_worker(orig, resized, spec, generator.settings)
  235. else:
  236. pool.apply_async(resize_worker, (orig, resized, spec, generator.settings))
  237. pool.close()
  238. pool.join()
  239. def detect_content(content):
  240. hrefs = None
  241. def replacer(m):
  242. what = m.group('what')
  243. value = m.group('value')
  244. origin = m.group('path')
  245. if what == 'photo':
  246. if value.startswith('/'):
  247. value = value[1:]
  248. path = os.path.join(
  249. os.path.expanduser(settings['PHOTO_LIBRARY']),
  250. value)
  251. if not os.path.isfile(path):
  252. logger.error('photos: No photo %s', path)
  253. else:
  254. photo = os.path.splitext(value)[0].lower() + 'a.jpg'
  255. origin = os.path.join(settings['SITEURL'], 'photos', photo)
  256. enqueue_resize(
  257. path,
  258. os.path.join('photos', photo),
  259. settings['PHOTO_ARTICLE'])
  260. return ''.join((m.group('markup'), m.group('quote'), origin,
  261. m.group('quote')))
  262. if hrefs is None:
  263. regex = r"""
  264. (?P<markup><\s*[^\>]* # match tag with src and href attr
  265. (?:href|src)\s*=)
  266. (?P<quote>["\']) # require value to be quoted
  267. (?P<path>{0}(?P<value>.*?)) # the url value
  268. \2""".format(content.settings['INTRASITE_LINK_REGEX'])
  269. hrefs = re.compile(regex, re.X)
  270. if content._content and '{photo}' in content._content:
  271. settings = content.settings
  272. content._content = hrefs.sub(replacer, content._content)
  273. def galleries_string_decompose(gallery_string):
  274. splitter_regex = re.compile(r'[\s,]*?({photo}|{filename})')
  275. title_regex = re.compile(r'{(.+)}')
  276. galleries = map(unicode.strip if sys.version_info.major == 2 else str.strip, filter(None, splitter_regex.split(gallery_string)))
  277. galleries = [gallery[1:] if gallery.startswith('/') else gallery for gallery in galleries]
  278. if len(galleries) % 2 == 0 and ' ' not in galleries:
  279. galleries = zip(zip(['type'] * len(galleries[0::2]), galleries[0::2]), zip(['location'] * len(galleries[0::2]), galleries[1::2]))
  280. galleries = [dict(gallery) for gallery in galleries]
  281. for gallery in galleries:
  282. title = re.search(title_regex, gallery['location'])
  283. if title:
  284. gallery['title'] = title.group(1)
  285. gallery['location'] = re.sub(title_regex, '', gallery['location']).strip()
  286. else:
  287. gallery['title'] = DEFAULT_CONFIG['PHOTO_GALLERY_TITLE']
  288. return galleries
  289. else:
  290. logger.error('Unexpected gallery location format! \n{}'.format(pprint.pformat(galleries)))
  291. def process_gallery(generator, content, location):
  292. content.photo_gallery = []
  293. galleries = galleries_string_decompose(location)
  294. for gallery in galleries:
  295. if gallery['location'] in DEFAULT_CONFIG['created_galleries']:
  296. content.photo_gallery.append((gallery['location'], DEFAULT_CONFIG['created_galleries'][gallery]))
  297. continue
  298. if gallery['type'] == '{photo}':
  299. dir_gallery = os.path.join(os.path.expanduser(generator.settings['PHOTO_LIBRARY']), gallery['location'])
  300. rel_gallery = gallery['location']
  301. elif gallery['type'] == '{filename}':
  302. base_path = os.path.join(generator.path, content.relative_dir)
  303. dir_gallery = os.path.join(base_path, gallery['location'])
  304. rel_gallery = os.path.join(content.relative_dir, gallery['location'])
  305. if os.path.isdir(dir_gallery):
  306. logger.info('photos: Gallery detected: {}'.format(rel_gallery))
  307. dir_photo = os.path.join('photos', rel_gallery.lower())
  308. dir_thumb = os.path.join('photos', rel_gallery.lower())
  309. exifs = read_notes(os.path.join(dir_gallery, 'exif.txt'),
  310. msg='photos: No EXIF for gallery')
  311. captions = read_notes(os.path.join(dir_gallery, 'captions.txt'), msg='photos: No captions for gallery')
  312. blacklist = read_notes(os.path.join(dir_gallery, 'blacklist.txt'), msg='photos: No blacklist for gallery')
  313. content_gallery = []
  314. title = gallery['title']
  315. for pic in sorted(os.listdir(dir_gallery)):
  316. if pic.startswith('.'):
  317. continue
  318. if pic.endswith('.txt'):
  319. continue
  320. if pic in blacklist:
  321. continue
  322. photo = os.path.splitext(pic)[0].lower() + '.jpg'
  323. thumb = os.path.splitext(pic)[0].lower() + 't.jpg'
  324. content_gallery.append((
  325. pic,
  326. os.path.join(dir_photo, photo),
  327. os.path.join(dir_thumb, thumb),
  328. exifs.get(pic, ''),
  329. captions.get(pic, '')))
  330. enqueue_resize(
  331. os.path.join(dir_gallery, pic),
  332. os.path.join(dir_photo, photo),
  333. generator.settings['PHOTO_GALLERY'])
  334. enqueue_resize(
  335. os.path.join(dir_gallery, pic),
  336. os.path.join(dir_thumb, thumb),
  337. generator.settings['PHOTO_THUMB'])
  338. content.photo_gallery.append((title, content_gallery))
  339. logger.debug('Gallery Data: '.format(pprint.pformat(content.photo_gallery)))
  340. DEFAULT_CONFIG['created_galleries']['gallery'] = content_gallery
  341. else:
  342. logger.error('photos: Gallery does not exist: {} at {}'.format(gallery['location'], dir_gallery))
  343. def detect_gallery(generator, content):
  344. if 'gallery' in content.metadata:
  345. gallery = content.metadata.get('gallery')
  346. if gallery.startswith('{photo}') or gallery.startswith('{filename}'):
  347. process_gallery(generator, content, gallery)
  348. elif gallery:
  349. logger.error('photos: Gallery tag not recognized: {}'.format(gallery))
  350. def image_clipper(x):
  351. return x[8:] if x[8] == '/' else x[7:]
  352. def file_clipper(x):
  353. return x[11:] if x[10] == '/' else x[10:]
  354. def process_image(generator, content, image):
  355. if image.startswith('{photo}'):
  356. path = os.path.join(os.path.expanduser(generator.settings['PHOTO_LIBRARY']), image_clipper(image))
  357. image = image_clipper(image)
  358. elif image.startswith('{filename}'):
  359. path = os.path.join(content.relative_dir, file_clipper(image))
  360. image = file_clipper(image)
  361. if os.path.isfile(path):
  362. photo = os.path.splitext(image)[0].lower() + 'a.jpg'
  363. thumb = os.path.splitext(image)[0].lower() + 't.jpg'
  364. content.photo_image = (
  365. os.path.basename(image).lower(),
  366. os.path.join('photos', photo),
  367. os.path.join('photos', thumb))
  368. enqueue_resize(
  369. path,
  370. os.path.join('photos', photo),
  371. generator.settings['PHOTO_ARTICLE'])
  372. enqueue_resize(
  373. path,
  374. os.path.join('photos', thumb),
  375. generator.settings['PHOTO_THUMB'])
  376. else:
  377. logger.error('photo: No photo for {} at {}'.format(content.source_path, path))
  378. def detect_image(generator, content):
  379. image = content.metadata.get('image', None)
  380. if image:
  381. if image.startswith('{photo}') or image.startswith('{filename}'):
  382. process_image(generator, content, image)
  383. else:
  384. logger.error('photos: Image tag not recognized: {}'.format(image))
  385. def detect_images_and_galleries(generators):
  386. """Runs generator on both pages and articles."""
  387. for generator in generators:
  388. if isinstance(generator, ArticlesGenerator):
  389. for article in itertools.chain(generator.articles, generator.drafts):
  390. detect_image(generator, article)
  391. detect_gallery(generator, article)
  392. elif isinstance(generator, PagesGenerator):
  393. for page in itertools.chain(generator.pages, generator.hidden_pages):
  394. detect_image(generator, page)
  395. detect_gallery(generator, page)
  396. def register():
  397. """Uses the new style of registration based on GitHub Pelican issue #314."""
  398. signals.initialized.connect(initialized)
  399. try:
  400. signals.content_object_init.connect(detect_content)
  401. signals.all_generators_finalized.connect(detect_images_and_galleries)
  402. signals.article_writer_finalized.connect(resize_photos)
  403. except Exception as e:
  404. logger.exception('Plugin failed to execute: {}'.format(pprint.pformat(e)))