From 80e124554d70cafedf147215dcb94cda0501f9f4 Mon Sep 17 00:00:00 2001 From: HoopyFreud Date: Sat, 8 Jul 2023 21:27:28 -0400 Subject: [PATCH] Update tile.py Accommodated no-source tiles better --- api/onnx_web/chain/tile.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/api/onnx_web/chain/tile.py b/api/onnx_web/chain/tile.py index 5e3aa2c7..54f47e12 100644 --- a/api/onnx_web/chain/tile.py +++ b/api/onnx_web/chain/tile.py @@ -232,18 +232,18 @@ def process_tile_spiral( **kwargs, ) -> Image.Image: width, height = kwargs.get("size", source.size if source else None) - + tiles: List[Tuple[int, int, Image.Image]] = [] # tile tuples is source, multiply by scale for dest counter = 0 tile_coords = generate_tile_spiral(width, height, tile, overlap=overlap) - + if len(tile_coords) == 1: single_tile = True else: single_tile = False - + for left, top in tile_coords: counter += 1 logger.info( @@ -268,13 +268,16 @@ def process_tile_spiral( if bottom > height: needs_margin = True bottom_margin = height - bottom - - if needs_margin: - # in the special case where the image is smaller than the specified tile size, just use the image + + #if no source given, we don't have a source image + if not source: + tile_image = None + elif needs_margin: + #in the special case where the image is smaller than the specified tile size, just use the image if single_tile: logger.debug("creating and processing single-tile subtile") tile_image = source - # otherwise use add histogram noise outside of the image border + #otherwise use add histogram noise outside of the image border else: logger.debug("tiling and adding margin") base_image = ( @@ -286,20 +289,18 @@ def process_tile_spiral( bottom - bottom_margin, ) ) - if source - else None ) tile_image = noise_source_histogram(base_image, (tile, tile), (0, 0)) tile_image.paste(base_image, (left_margin, top_margin)) else: logger.debug("tiling normally") - tile_image = source.crop((left, top, right, bottom)) if source else None + tile_image = source.crop((left, top, right, bottom)) for image_filter in filters: tile_image = image_filter(tile_image, (left, top, tile)) tiles.append((left, top, tile_image)) - + if single_tile: return tile_image else: @@ -353,8 +354,11 @@ def generate_tile_spiral( # calculate the start position of the tiling span_x = tile + (width_tile_target - 1) * tile_increment span_y = tile + (height_tile_target - 1) * tile_increment - - logger.debug("tiled image overlap: %s. Span: %s x %s", overlap, span_x, span_y) + + logger.debug( + "tiled image overlap: %s. Span: %s x %s", + overlap,span_x,span_y + ) tile_left = ( width - span_x