Source code for egaia.egaia_collage

import shutil
import os
from random import shuffle

import egaia_list
import egaia_root
import egaia_parsefn
from egaia_config import getConfig
from utils import run

[docs]def getFilenamesFromUuids(uuids): """Retrieve a list of filenames from a list of UUIDs.""" filenames = list() for uuid in uuids: filepath = egaia_list.listFiles(uuid=uuid, filter_type='df-med-img')[0] # listFiles will return None if there is no match; e.g., if the # supplied uuid is incorrect if not filepath: continue filenames.append(filepath) return filenames
[docs]def getFilenames(uuids=None): """Create a list of filepaths for items in the current collection that have thumbnail images available. """ filenames = egaia_list.listFiles(filter_type='df-med-img') return filenames
[docs]def resize(src, target, size, border=None): """Create a resized image for the collage.""" cmd_convert = getConfig('system', 'cmd_convert') target = os.path.join(egaia_root.get_root(), target) try: r = run([cmd_convert, src, '-thumbnail', '%s^' % size, '-gravity', 'center', '-extent', size, '-bordercolor', 'white', '-border', '2x2', target]) except: r = False return r
[docs]def mkcollage(filenames=None, prefix=''): """Generate a collage out of a list of images.""" if not filenames: filenames = getFilenames() small = prefix + 'collection-thumb.jpg' large = prefix + 'collection-cover.jpg' if len(filenames) == 0: return False elif len(filenames) < 5: resize(filenames[0], target = small, size = '596x296') resize(filenames[0], target = large, size = '1196x396') return True shuffle(filenames) # A: left and right cells, 400x400. Account for a 1px border. resize(filenames[0], target = '_AL.jpg', size = '396x396') resize(filenames[1], target = '_AR.jpg', size = '396x396') # B: top right row, 400x200. resize(filenames[2], target = '_B.jpg', size = '396x196') # CD: bottom right row, 200x200 and 200x200 resize(filenames[3], target = '_C.jpg', size = '196x196') resize(filenames[4], target = '_D.jpg', size = '196x196') # build the montage cmd_montage = getConfig('system', 'cmd_montage') run([cmd_montage, '_C.jpg', '_D.jpg', '-geometry', '200x200+0+0', '-tile', '2x1', '_CD.jpg']) run([cmd_montage, '_B.jpg', '_CD.jpg', '-geometry', '400x200+0+0', '-tile', '1x2', '_BCD.jpg']) # small image run([cmd_montage, '_AL.jpg', '_BCD.jpg', '-geometry', '300x300+0+0', '-tile', '2x1', small]) # large montage run([cmd_montage, '_AL.jpg', '_BCD.jpg', '_AR.jpg', '-geometry', '400x400+0+0', '-tile', '3x1', large]) for f in ('_AL.jpg', '_AR.jpg', '_B.jpg', '_C.jpg', '_D.jpg', '_CD.jpg', '_BCD.jpg' ): os.unlink(f) return True
def _cli(args): """egaia collage Generate a collage from a series of images representing the items in a collection. Usage: egaia collage --help egaia collage [ --prefix=PREFIX ] [ ITEM... ] """ if not egaia_root.get_root(): exit('Please run "egaia init" to create a new collection here.') if args['ITEM']: uuids = list() for item in args['ITEM']: uuids.append(egaia_parsefn.getUuid(item)) # although input may already be filenames, we actually want to get the # thumbnail images, and user input may be something else filenames = getFilenamesFromUuids(uuids) else: filenames = None prefix = args['--prefix'] if not prefix: prefix = '' return mkcollage(filenames, prefix)