Skip to content

Commit

Permalink
fix(api): switch between spiral and grid tiling based on outpaint mar…
Browse files Browse the repository at this point in the history
…gins (#101)
  • Loading branch information
ssube committed Feb 5, 2023
1 parent ca2f0a6 commit 6fe278c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
13 changes: 11 additions & 2 deletions api/onnx_web/chain/upscale_outpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ..output import save_image
from ..params import Border, ImageParams, Size, SizeChart, StageParams
from ..utils import ServerContext, is_debug
from .utils import process_tile_spiral
from .utils import process_tile_grid, process_tile_spiral

logger = getLogger(__name__)

Expand Down Expand Up @@ -92,7 +92,16 @@ def outpaint(image: Image.Image, dims: Tuple[int, int, int]):
draw_mask.rectangle((left, top, left + tile, top + tile), fill="black")
return result.images[0]

output = process_tile_spiral(source_image, SizeChart.auto, 1, [outpaint])
margin_x = float(max(border.left, border.right))
margin_y = float(max(border.top, border.bottom))
overlap = min(margin_x / source_image.width, margin_y / source_image.height)

if overlap > 0 and border.left == border.right and border.top == border.bottom:
logger.debug("outpainting with an even border, using spiral tiling")
output = process_tile_spiral(source_image, SizeChart.auto, 1, [outpaint], overlap=overlap)
else:
logger.debug("outpainting with an uneven border, using grid tiling")
output = process_tile_grid(source_image, SizeChart.auto, 1, [outpaint])

logger.info("final output image size: %sx%s", output.width, output.height)
return output
23 changes: 11 additions & 12 deletions api/onnx_web/chain/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,17 @@ def process_tile_spiral(
center_x = (width // 2) - (tile // 2)
center_y = (height // 2) - (tile // 2)

# TODO: only valid for overlap = 0.5
if overlap == 0.5:
tiles = [
(0, tile * -overlap),
(tile * overlap, tile * -overlap),
(tile * overlap, 0),
(tile * overlap, tile * overlap),
(0, tile * overlap),
(tile * -overlap, tile * overlap),
(tile * -overlap, 0),
(tile * -overlap, tile * -overlap),
]
# TODO: should add/remove tiles when overlap != 0.5
tiles = [
(0, tile * -overlap),
(tile * overlap, tile * -overlap),
(tile * overlap, 0),
(tile * overlap, tile * overlap),
(0, tile * overlap),
(tile * -overlap, tile * overlap),
(tile * -overlap, 0),
(tile * -overlap, tile * -overlap),
]

# tile tuples is source, multiply by scale for dest
counter = 0
Expand Down

0 comments on commit 6fe278c

Please sign in to comment.