def build_image_cache(ic, min_rating, dest, tmp_path, preview_size=900): # build preview cache for 5* images... for k, e in ic.items(): name = e.get('name', None) xmps = e.get('xmp', None) jpegs = e.get('jpeg', None) raws = e.get('raw', None) ##!!! get tmp dir... ##!!! res = { 'missing preview': [], 'missing raw': [], 'unrated': [], } if xmps is not None: ##!!! avoid manual path forming.... xmp_path = xmps[0][0][0] + '\\' + os.path.join( *(xmps[0][0][1:] + [name])) + '.' + xmps[0][1] # get rating... im = metadata.ImageMetadata(xmp_path) im.read() rating = im['Xmp.xmp.Rating'].value ##!!! cache the rating... e['Rating'] = rating ic[k] = e if rating >= min_rating: # get the jpeg... if jpegs is None: if raws is None: ## print '### can\'t find raws for %s' % name res['missing raw'] += [k] continue raw_path = raws[0][0][0] + '\\' + os.path.join( *(raws[0][0][1:] + [name])) + '.' + raws[0][1] ## print '>> missing preview %s.jpg' % name res['missing preview'] += [raw_path] ##!!! generate jpegs... raw = metadata.ImageMetadata(raw_path) raw.read() for i, p in enumerate(raw.previews): if max(p.dimensions) >= preview_size: # extract preview... tmp_preview_path = os.path.join( tmp_path, '%s-%s%s' % (name, i, p.extension)) open(tmp_preview_path, 'wb').write(p.data) # resize preview... orig = Image.open(tmp_preview_path) scale = preview_size / float(max(*orig.size)) print 'generating preview...' preview = orig.resize((int(orig.size[0] * scale), int(orig.size[1] * scale)), Image.ANTIALIAS) # save preview... print 'saving preview: %s' % name preview_path = os.path.join( dest, k + '-' + name + '.jpg') preview.save(preview_path) # copy metadata... ##!!! need to write metadata to all previews... preview_metadata = metadata.ImageMetadata( preview_path) preview_metadata.read() raw.copy(preview_metadata) preview_metadata.write() # remove temporary files... ##!!! triple-check... try: os.remove(tmp_preview_path) except: pass else: jpg_path = jpegs[0][0][0] + '\\' + os.path.join( *(jpegs[0][0][1:] + [name])) + '.' + jpegs[0][1] # copy the jpeg to the cache... print '>>> copy: %s.jpg' % name ##!!! HACK: manual name generation... ##!!! use a good id, like a timestamp... shutil.copy2(jpg_path, os.path.join(dest, k + '-' + name + '.jpg')) else: ##!!! need to detect unrated shoots... ## print '>> no XMP' res['unrated'] += [k] continue ic.cache_flush() store.pack(ic._path, keep_files=False) return res
def build_image_cache(ic, min_rating, dest, tmp_path, preview_size=900): # build preview cache for 5* images... for k, e in ic.items(): name = e.get('name', None) xmps = e.get('xmp', None) jpegs = e.get('jpeg', None) raws = e.get('raw', None) ##!!! get tmp dir... ##!!! res = { 'missing preview': [], 'missing raw': [], 'unrated': [], } if xmps is not None: ##!!! avoid manual path forming.... xmp_path = xmps[0][0][0] + '\\' + os.path.join(*(xmps[0][0][1:] + [name])) + '.' + xmps[0][1] # get rating... im = metadata.ImageMetadata(xmp_path) im.read() rating = im['Xmp.xmp.Rating'].value ##!!! cache the rating... e['Rating'] = rating ic[k] = e if rating >= min_rating: # get the jpeg... if jpegs is None: if raws is None: ## print '### can\'t find raws for %s' % name res['missing raw'] += [k] continue raw_path = raws[0][0][0] + '\\' + os.path.join(*(raws[0][0][1:] + [name])) + '.' + raws[0][1] ## print '>> missing preview %s.jpg' % name res['missing preview'] += [raw_path] ##!!! generate jpegs... raw = metadata.ImageMetadata(raw_path) raw.read() for i, p in enumerate(raw.previews): if max(p.dimensions) >= preview_size: # extract preview... tmp_preview_path = os.path.join(tmp_path, '%s-%s%s' % (name, i, p.extension)) open(tmp_preview_path, 'wb').write(p.data) # resize preview... orig = Image.open(tmp_preview_path) scale = preview_size/float(max(*orig.size)) print 'generating preview...' preview = orig.resize((int(orig.size[0]*scale), int(orig.size[1]*scale)), Image.ANTIALIAS) # save preview... print 'saving preview: %s' % name preview_path = os.path.join(dest, k + '-' + name + '.jpg') preview.save(preview_path) # copy metadata... ##!!! need to write metadata to all previews... preview_metadata = metadata.ImageMetadata(preview_path) preview_metadata.read() raw.copy(preview_metadata) preview_metadata.write() # remove temporary files... ##!!! triple-check... try: os.remove(tmp_preview_path) except: pass else: jpg_path = jpegs[0][0][0] + '\\' + os.path.join(*(jpegs[0][0][1:] + [name])) + '.' + jpegs[0][1] # copy the jpeg to the cache... print '>>> copy: %s.jpg' % name ##!!! HACK: manual name generation... ##!!! use a good id, like a timestamp... shutil.copy2(jpg_path, os.path.join(dest, k + '-' + name + '.jpg')) else: ##!!! need to detect unrated shoots... ## print '>> no XMP' res['unrated'] += [k] continue ic.cache_flush() store.pack(ic._path, keep_files=False) return res
l = len(index) index = dict(index) print l, len(index) json.dump(index, file(os.path.join('test', 'filelist.json'), 'w')) root_index = store.dump(index, os.path.join('test', 'index'), index_depth=1) ## ##!!! this is not used in anything yet... ## json.dump(root_index, file(os.path.join('test', 'index', 'file_index.json'), 'w')) store.pack(os.path.join('test', 'index'), keep_files=False) d = store.load(os.path.join('test', 'index')) print len(d) k = d.keys()[0] i = store.Index(os.path.join('test', 'index')) print len(i) ## print i[k] ic = store.IndexWithCache(os.path.join('test', 'index'))