"""This script will search a dir structure for image files which match a list of upc's, and convert and move them.""" # Walk root directory import os import re from PIL import Image from integration_scripts import logger from integration_scripts.general_use import dedupe_list # SOURCE_PATH = '/Volumes/SuburbanCat/The Orchard' SOURCE_PATH = \ '/Volumes/ripped_assets/ripper_feeder_staging/Suburban/unconformed' TARGET_PATH = '/Volumes/ripped_assets/ripper_feeder_staging/Suburban/' \ 'staging/INT-484 Final Push' # Both valid and invalid image formats in groups. audio_folder = r'(?P\d{1,13})(?P[\ \-\_]{1,3})(?P.*)' cover_file_extensions = r'(?P^[^\.\_]\d{1,13}\.(' \ r'jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG|TIF|tif|TIFF' \ r'|tiff|bmp|BMP))' track_input_file_name = None upc_input_file_name = '/Users/lvona/Documents/python_projects/ripper' \ '-feeder/data/INT-477 - Final Push - Covers.txt' def save_image(asset_target_file, image): """Save image.""" # if the image is not square if image.width != image.height: # Check how much longer the long side is long_edge = max(image.width, image.height) short_edge = min(image.width, image.height) percent_larger = (abs(short_edge - long_edge) / short_edge) * 100 thresh = 15 force_square = True # Reshape if necessary and within threshold if force_square and percent_larger <= thresh: image = image.resize((long_edge, long_edge)) else: raise RuntimeError( 'Image {} is not square, and has a long edge over {}% ' 'longer than the short edge. Will not auto-resize.'.format( asset_target_file, thresh)) if image.mode != 'RGB': logger.bind(file=True).info('Converting {} to RGB.', asset_target_file) image = image.convert('RGB') # Upscale dimensions if image.width < 3000 or image.width > 6000: logger.bind(file=True).info( 'Upscaling image {} to 3000x3000px.', asset_target_file) image = image.resize((3000, 3000)) logger.bind(file=True).info( 'Saving File (at 300dpi): {}', asset_target_file) # Save as new file (extension determines type) image.save(asset_target_file, dpi=(300, 300)) def load_metadata(upcs=None): if not upcs: upcs = list() # Get list of tracks from the metadata infile. # metadata_track_list = [] metadata_upc_list = [] # if track_input_file_name: # with open(track_input_file_name) as f: # metadata_track_list = f.readlines() # # # Clean off trailing '\n's # metadata_track_list = [x.rstrip('\n') for x in metadata_track_list] # TODO - Check all track files fit 'UPC_VOL_TRACK.WAV' pattern # if get_metadata_structure(data_source_folder): # logger.info('Metadata has tracks with bad structure.') # report_bad_structure(metadata_track_list) # metadata_track_list = remove_bad_tracks(metadata_track_list) if len(upcs): metadata_upc_list = upcs elif upc_input_file_name: # Get list of UPC's from the metadata infile. with open(upc_input_file_name) as f: metadata_upc_list = f.readlines() metadata_upc_list = [x.rstrip('\n') for x in metadata_upc_list] # De-dupe UPC's metadata_upc_list = dedupe_list(metadata_upc_list) # Report UPC's logger.bind(file=True).warning( '{} unique UPC\'s found in Metadata', len(metadata_upc_list)) return metadata_upc_list def main(): inline_list = ['8716059010953'] upc_list = load_metadata(inline_list) logger.bind(file=True).info( 'Creating folder {} if it does not exist.', TARGET_PATH) os.makedirs(TARGET_PATH, exist_ok=True) # root = '/Volumes/SuburbanCat/The Orchard' for path, subdirs, files in os.walk(SOURCE_PATH): image_list = [] current_dir = path.split('/')[-1] # Check for upc in folder check_upc = re.match(audio_folder, current_dir) # If valid folder. if check_upc: folder_upc = check_upc.group('upc') if folder_upc not in upc_list: # logger.bind(file=True).error( # '{} is not in ingest list.', folder_upc) continue for file in files: check_audio = re.match(cover_file_extensions, file) if check_audio: image_list.append(file) image_file = check_audio.group('image_file') file_upc = image_file.split('.')[0] if file_upc != folder_upc: logger.bind(file=True).info( '{} is not in matching folder. ' 'It is in folder {} instead.', file, folder_upc) else: try: logger.bind(file=True).info( 'Opening file {}.', file) image = Image.open(os.path.join(path, file)) except Exception as e: # Open and convert fails logger.bind(file=True).error( 'Could not open file: {}', str(e)) else: try: target_path = os.path.join( TARGET_PATH, file_upc + '.tif') save_image(target_path, image) except Exception as e: logger.bind(file=True).error( 'Could not save file: {}', str(e)) if not len(image_list): logger.bind(file=True).info( '{}: No valid image file found in directory.', folder_upc) else: logger.bind(file=True).info( '{} is not a valid UPC folder.', current_dir) for image in image_list: logger.bind(file=True).info(image) if __name__ == '__main__': main()