def medium_expo_thumbnail(force=False, remove_zoom=False, rebuild_zoom=False): tag = 'expo' qfix = query.get_table_query('datadocs') for element in qfix.filter({'type': tag}).run(): images = element.pop('images', {}) if len(images) < 1: continue image = images.pop() # from beeprint import pp # pp(image) fname = image.get('filename') absf = os.path.join(UPLOAD_FOLDER, tag, fname) filebase, fileext = os.path.splitext(absf) small = filebase + '.small.jpg' if not os.path.exists(small) or rebuild_zoom: ori = os.path.join(filebase, 'TileGroup0', '0-0-0.jpg') # Zoom if not exists if not os.path.exists(filebase) or rebuild_zoom: if not zoomer.process_zoom(absf): raise BaseException("Failed to zoom file '%s'" % fname) logger.info("Zoomed image '%s'" % filebase) # Copy 0.0.0 as filename.small.jpg shutil.copyfile(ori, small) # shutil.move(ori, small) logger.debug("Created thumb %s" % small) # Remove zoom dir if remove_zoom: try: shutil.rmtree(filebase) logger.debug("Removed dir '%s' " % filebase) except Exception as e: logger.critical("Cannot remove zoomified:\n '%s'" % str(e)) # Build with 'convert' binary a filename.medium.jpg medium = filebase + '.medium.jpg' size = '25' if force or not os.path.exists(medium): from plumbum.cmd import convert convert(['-resize', size + '%', absf, medium]) logger.info("Builded %s with %s %s dimension" % (medium, '%', size))
def build_zoom(force=False): import re # pattern = re.compile("^[0-9]+$") q = query.get_table_query(t3in) for record in q.filter({'type': 'documents'}).run(): images = record.pop('images') if len(images) < 1: continue image = images.pop() ################## # FIX ZOOM for files like [0-9]+.jpg # match = pattern.match(image['code']) # if match is None: # continue ################## abs_file = os.path.join(UPLOAD_FOLDER, image['filename']) # Remove zoomified directory filebase, fileext = os.path.splitext(abs_file) if force and os.path.exists(filebase): try: shutil.rmtree(filebase) logger.debug("Removed dir '%s' " % filebase) except Exception as e: logger.critical("Cannot remove zoomified:\n '%s'" % str(e)) if not os.path.exists(filebase): logger.debug("Converting %s" % abs_file) if not zoomer.process_zoom(abs_file): raise BaseException( "Failed to zoom file '%s'" % image['filename']) logger.info("Zoomed image '%s'" % image['filename']) ################## # FIX TIFF if not image['filename'][-4:] == '.tif': continue logger.warning("Converting current image as tiff") path = os.path.join('/uploads', image['filename']) if not os.path.exists(path): raise BaseException("Cannot find registered path %s" % path) newpath = path.replace('.tif', '.jpg') if force or os.path.exists(newpath): # Convert tif to jpg from plumbum.cmd import convert convert([path, newpath]) logger.info("Converted TIF to %s" % newpath) # Update image['filename'] = image['filename'].replace('.tif', '.jpg') record['images'] = [image] changes = q.get(record['record']).replace(record).run() print("Changes", changes) logger.debug("Updated %s" % record['record'])