1
0
Fork 0

feat(api): add a mask filter to blend outpainting edges

This commit is contained in:
Sean Sube 2023-01-15 09:54:55 -06:00
parent c0bef9a1a7
commit 6c07d124e0
1 changed files with 28 additions and 6 deletions

View File

@ -1,5 +1,5 @@
from numpy import random from numpy import random
from PIL import Image, ImageFilter from PIL import Image, ImageChops, ImageFilter
from typing import Tuple from typing import Tuple
import numpy as np import numpy as np
@ -44,6 +44,23 @@ def blend_source_mask(source: Tuple[int, int, int], mask: Tuple[int, int, int],
) )
def mask_filter_gaussian(mask_image: Image, dims: Tuple[int, int], origin: Tuple[int, int], rounds=3) -> Tuple[float, float, float]:
'''
Gaussian blur, source image centered on white canvas.
'''
width, height = dims
noise = Image.new('RGB', (width, height), 'white')
noise.paste(mask_image, origin)
for i in range(rounds):
blur = noise.copy()
blur.filter(ImageFilter.GaussianBlur(5))
noise = ImageChops.screen(noise, blur)
return noise
def noise_source_fill(source_image: Image, dims: Tuple[int, int], origin: Tuple[int, int], fill='white') -> Tuple[float, float, float]: def noise_source_fill(source_image: Image, dims: Tuple[int, int], origin: Tuple[int, int], fill='white') -> Tuple[float, float, float]:
''' '''
Identity transform, source image centered on white canvas. Identity transform, source image centered on white canvas.
@ -152,6 +169,7 @@ def expand_image(
expand_by: Tuple[int, int, int, int], expand_by: Tuple[int, int, int, int],
fill='white', fill='white',
noise_source=noise_source_histogram, noise_source=noise_source_histogram,
mask_filter=mask_filter_gaussian,
blend_op=blend_source_mask blend_op=blend_source_mask
): ):
left, right, top, bottom = expand_by left, right, top, bottom = expand_by
@ -159,13 +177,17 @@ def expand_image(
full_width = left + source_image.width + right full_width = left + source_image.width + right
full_height = top + source_image.height + bottom full_height = top + source_image.height + bottom
full_source = Image.new('RGB', (full_width, full_height), fill) dims = (full_width, full_height)
full_source.paste(source_image, (left, top)) origin = (top, left)
full_mask = Image.new('RGB', (full_width, full_height), fill) full_source = Image.new('RGB', dims, fill)
full_mask.paste(mask_image, (left, top)) full_source.paste(source_image, origin)
full_noise = noise_source(source_image, (full_width, full_height), (top, left)) full_mask = Image.new('RGB', dims, fill)
full_mask = mask_filter_gaussian(full_mask, dims, origin)
full_mask.paste(mask_image, origin)
full_noise = noise_source(source_image, dims, origin)
for x in range(full_source.width): for x in range(full_source.width):
for y in range(full_source.height): for y in range(full_source.height):