(**kwargs)
return [images, metadata]
Main method for filtering images given user metadata (if provided)
Main method for filtering images given user metadata (if provided)
Main method for filtering images given user metadata ( if provided )
def filter_images(**kwargs):
  '''Main method for filtering images given user metadata (if provided)'''
  # validate that input image names are unique
  image_paths = set()
  duplicates = set()
  for i in stream_images(image_paths=get_image_paths(**kwargs)):
    if i.path in image_paths:
      duplicates.add(i.path)
    image_paths.add(i.path)
  if duplicates:
    raise Exception('''
      Image filenames should be unique, but the following filenames are duplicated\n
      {}
      '''.format('\n'.join(duplicates)))
  if not kwargs.get('shuffle', False):
    image_paths = sorted(image_paths)
  # process and filter the images
  filtered_image_paths = []
  for i in stream_images(image_paths=image_paths):
    # get image height and width
    w, h = i.original.size
    # remove images with 0 height or width when resized to lod height
    if (h == 0) or (w == 0):
      print(timestamp(), 'Skipping {} because it contains 0 height or width'.format(i.path))
      continue
    # remove images that have 0 height or width when resized
    try:
      resized = i.resize_to_max(kwargs['lod_cell_height'])
    except ValueError:
      print(timestamp(), 'Skipping {} because it contains 0 height or width when resized'.format(i.path))
      continue
    except OSError:
      print(timestamp(), 'Skipping {} because it could not be resized'.format(i.path))
      continue
    # remove images that are too wide for the atlas
    if (w/h) > (kwargs['atlas_size']/kwargs['cell_size']):
      print(timestamp(), 'Skipping {} because its dimensions are oblong'.format(i.path))
      continue
    filtered_image_paths.append(i.path)
  # if there are no remaining images, throw an error
  if len(filtered_image_paths) == 0:
    raise Exception('No images were found! Please check your input image glob.')
  # handle the case user provided no metadata
  if not kwargs.get('metadata', False):
    return [filtered_image_paths, []]
  # handle user metadata: retain only records with image and metadata
  l = get_metadata_list(**kwargs)
  meta_bn = set([clean_filename(i.get('filename', '')) for i in l])
  img_bn = set([clean_filename(i, **kwargs) for i in filtered_image_paths])
  # identify images with metadata and those without metadata
  meta_present = img_bn.intersection(meta_bn)
  meta_missing = list(img_bn - meta_bn)
  # notify the user of images that are missing metadata
  if meta_missing:
    print(timestamp(), ' ! Some images are missing metadata:\n  -', '\n  - '.join(meta_missing[:10]))
    if len(meta_missing) > 10: print(timestamp(), ' ...', len(meta_missing)-10, 'more')
    with open('missing-metadata.txt', 'w') as out: out.write('\n'.join(meta_missing))
  # get the sorted lists of images and metadata
  d = {clean_filename(i['filename']): i for i in l}
  images = []
  metadata = []
  for i in filtered_image_paths:
    if clean_filename(i, **kwargs) in meta_present:
      images.append(i)
      metadata.append(copy.deepcopy(d[clean_filename(i, **kwargs)]))
  kwargs['metadata'] = metadata
  write_metadata(**kwargs)
  return [images, metadata]
def filter_images ( * * kwargs ) : # validate that input image names are unique image_paths = set ( ) duplicates = set ( ) for i in stream_images ( image_paths = get_image_paths ( * * kwargs ) ) : if i . path in image_paths : duplicates . add ( i . path ) image_paths . add ( i . path ) if duplicates : raise Exception ( '''
      Image filenames should be unique, but the following filenames are duplicated\n
      {}
      ''' . format ( '\n' . join ( duplicates ) ) ) if not kwargs . get ( 'shuffle' , False ) : image_paths = sorted ( image_paths ) # process and filter the images filtered_image_paths = [ ] for i in stream_images ( image_paths = image_paths ) : # get image height and width w , h = i . original . size # remove images with 0 height or width when resized to lod height if ( h == 0 ) or ( w == 0 ) : print ( timestamp ( ) , 'Skipping {} because it contains 0 height or width' . format ( i . path ) ) continue # remove images that have 0 height or width when resized try : resized = i . resize_to_max ( kwargs [ 'lod_cell_height' ] ) except ValueError : print ( timestamp ( ) , 'Skipping {} because it contains 0 height or width when resized' . format ( i . path ) ) continue except OSError : print ( timestamp ( ) , 'Skipping {} because it could not be resized' . format ( i . path ) ) continue # remove images that are too wide for the atlas if ( w / h ) > ( kwargs [ 'atlas_size' ] / kwargs [ 'cell_size' ] ) : print ( timestamp ( ) , 'Skipping {} because its dimensions are oblong' . format ( i . path ) ) continue filtered_image_paths . append ( i . path ) # if there are no remaining images, throw an error if len ( filtered_image_paths ) == 0 : raise Exception ( 'No images were found! Please check your input image glob.' ) # handle the case user provided no metadata if not kwargs . get ( 'metadata' , False ) : return [ filtered_image_paths , [ ] ] # handle user metadata: retain only records with image and metadata l = get_metadata_list ( * * kwargs ) meta_bn = set ( [ clean_filename ( i . get ( 'filename' , '' ) ) for i in l ] ) img_bn = set ( [ clean_filename ( i , * * kwargs ) for i in filtered_image_paths ] ) # identify images with metadata and those without metadata meta_present = img_bn . intersection ( meta_bn ) meta_missing = list ( img_bn - meta_bn ) # notify the user of images that are missing metadata if meta_missing : print ( timestamp ( ) , ' ! Some images are missing metadata:\n  -' , '\n  - ' . join ( meta_missing [ : 10 ] ) ) if len ( meta_missing ) > 10 : print ( timestamp ( ) , ' ...' , len ( meta_missing ) - 10 , 'more' ) with open ( 'missing-metadata.txt' , 'w' ) as out : out . write ( '\n' . join ( meta_missing ) ) # get the sorted lists of images and metadata d = { clean_filename ( i [ 'filename' ] ) : i for i in l } images = [ ] metadata = [ ] for i in filtered_image_paths : if clean_filename ( i , * * kwargs ) in meta_present : images . append ( i ) metadata . append ( copy . deepcopy ( d [ clean_filename ( i , * * kwargs ) ] ) ) kwargs [ 'metadata' ] = metadata write_metadata ( * * kwargs ) return [ images , metadata ]