How to use the handprint.files.filename_basename function in handprint

To help you get started, we’ve selected a few handprint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github caltechlibrary / handprint / handprint / manager.py View on Github external
def _converted_file(self, file, to_format, dest_dir):
        basename = path.basename(filename_basename(file))
        new_file = path.join(dest_dir, basename + '.handprint.' + to_format)
        if path.exists(new_file):
            inform('Using existing converted image in {}', relative(new_file))
            return new_file
        else:
            inform('Converting to {} format: {}', to_format, relative(file))
            (converted, error) = converted_image(file, to_format, new_file)
            if error:
                alert('Failed to convert {}: {}', relative(file), error)
                return None
            return converted
github caltechlibrary / handprint / handprint / manager.py View on Github external
def _smaller_file(self, file):
        if not file:
            return None
        file_ext = filename_extension(file)
        name_tail = '.handprint' + file_ext
        new_file = file if name_tail in file else filename_basename(file) + name_tail
        if path.exists(new_file):
            if image_size(new_file) < self._max_size:
                inform('Reusing resized image found in {}', relative(new_file))
                return new_file
            else:
                # We found a ".handprint.ext" file, perhaps from a previous run,
                # but for the current set of services, it's larger than allowed.
                if __debug__: log('existing resized file larger than {}b: {}',
                                  humanize.intcomma(self._max_size), new_file)
        inform('Size too large; reducing size: {}', relative(file))
        (resized, error) = reduced_image_size(file, new_file, self._max_size)
        if error:
            alert('Failed to resize {}: {}', relative(file), error)
            return None
        return resized
github caltechlibrary / handprint / handprint / main_body.py View on Github external
targets += files_in_directory(item, extensions = ACCEPTED_FORMATS)
                else:
                    warn('"{}" not a file or directory', item)

        # Filter files created in past runs.
        targets = filter(lambda name: '.handprint' not in name, targets)

        # If there is both a file in the format we generate and another
        # format of that file, ignore the other formats and just use ours.
        # Note: the value of targets is an iterator, but b/c it's tested inside
        # the loop, a separate list is needed (else get unexpected results).
        targets = list(targets)
        keep = []
        for item in targets:
            ext  = filename_extension(item)
            base = filename_basename(item)
            if ext != _OUTPUT_EXT and (base + _OUTPUT_EXT in targets):
                # png version of file is also present => skip this other version
                continue
            keep.append(item)
        return keep
github caltechlibrary / handprint / handprint / manager.py View on Github external
def _resized_image(self, file):
        (max_width, max_height) = self._max_dimensions
        file_ext = filename_extension(file)
        name_tail = '.handprint' + file_ext
        new_file = file if name_tail in file else filename_basename(file) + name_tail
        if path.exists(new_file) and readable(new_file):
            (image_width, image_height) = image_dimensions(new_file)
            if image_width < max_width and image_height < max_height:
                inform('Using reduced image found in {}', relative(new_file))
                return new_file
            else:
                # We found a "-reduced" file, perhaps from a previous run, but
                # for the current set of services, dimension are too large.
                if __debug__: log('existing resized file larger than {}x{}: {}',
                                  max_width, max_height, new_file)
        inform('Dimensions too large; reducing dimensions: {}', relative(file))
        (resized, error) = reduced_image_dimensions(file, new_file, max_width, max_height)
        if error:
            alert('Failed to re-dimension {}: {}', relative(file), error)
            return None
        return resized
github caltechlibrary / handprint / handprint / manager.py View on Github external
# Send the file to the services and get Result tuples back.
            if self._num_threads == 1:
                # For 1 thread, avoid thread pool to make debugging easier.
                results = [self._send(image, s) for s in services]
            else:
                with ThreadPoolExecutor(max_workers = self._num_threads) as tpe:
                    results = list(tpe.map(self._send, repeat(image), iter(services)))

            # If a service failed for some reason (e.g., a network glitch), we
            # get no result back.  Remove empty results & go on with the rest.
            results = [x for x in results if x is not None]

            # Create grid file if requested.
            if self._make_grid:
                base = path.basename(filename_basename(item_file))
                grid_file = path.realpath(path.join(dest_dir, base + '.handprint-all.png'))
                inform('Creating results grid image: {}', relative(grid_file))
                all_results = [r.annotated for r in results]
                width = math.ceil(math.sqrt(len(all_results)))
                create_image_grid(all_results, grid_file, max_horizontal = width)

            # Clean up after ourselves.
            if not self._extended_results:
                for file in set(image.temp_files | {r.annotated for r in results}):
                    if file and path.exists(file):
                        delete_existing(file)

            inform('Done with {}', relative(item))
        except (KeyboardInterrupt, UserCancelled) as ex:
            warn('Interrupted')
            raise