Skip to content

Commit

Permalink
feat(api): add normal and uniform noise sources
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 15, 2023
1 parent 0d53fdf commit 9376de8
Showing 1 changed file with 73 additions and 12 deletions.
85 changes: 73 additions & 12 deletions api/onnx_web/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np


def blend_mult(a):
return float(a) / 256

Expand All @@ -13,14 +14,64 @@ def blend_imult(a):


def blend_mask_inverse_source(source: Tuple[int, int, int], mask: Tuple[int, int, int], noise: Tuple[int, int, int]) -> Tuple[int, int, int]:
'''
Blend operation, linear interpolation from source to noise based on mask: `(s * (1 - m)) + (n * m)`
'''
return (
int((source[0] * blend_imult(mask[0])) + (noise[0] * blend_mult(mask[0]))),
int((source[1] * blend_imult(mask[1])) + (noise[1] * blend_mult(mask[1]))),
int((source[2] * blend_imult(mask[2])) + (noise[2] * blend_mult(mask[2]))),
int((source[0] * blend_imult(mask[0])) +
(noise[0] * blend_mult(mask[0]))),
int((source[1] * blend_imult(mask[1])) +
(noise[1] * blend_mult(mask[1]))),
int((source[2] * blend_imult(mask[2])) +
(noise[2] * blend_mult(mask[2]))),
)


def blend_source_histogram(source_image: Image, dims: Tuple[int, int]) -> Tuple[float, float, float]:
def noise_source_uniform(source_image: Image, dims: Tuple[int, int]) -> Tuple[float, float, float]:
width, height = dims
size = width * height

noise_r = random.uniform(0, 256, size=size)
noise_g = random.uniform(0, 256, size=size)
noise_b = random.uniform(0, 256, size=size)

noise = Image.new('RGB', (width, height))

for x in range(width):
for y in range(height):
i = x * y
noise.putpixel((x, y), (
int(noise_r[i]),
int(noise_g[i]),
int(noise_b[i])
))

return noise


def noise_source_gaussian(source_image: Image, dims: Tuple[int, int]) -> Tuple[float, float, float]:
width, height = dims
size = width * height

noise_r = random.normal(128, 32, size=size)
noise_g = random.normal(128, 32, size=size)
noise_b = random.normal(128, 32, size=size)

noise = Image.new('RGB', (width, height))

for x in range(width):
for y in range(height):
i = x * y
noise.putpixel((x, y), (
int(noise_r[i]),
int(noise_g[i]),
int(noise_b[i])
))

return noise


def noise_source_histogram(source_image: Image, dims: Tuple[int, int]) -> Tuple[float, float, float]:
r, g, b = source_image.split()
width, height = dims
size = width * height
Expand All @@ -29,9 +80,12 @@ def blend_source_histogram(source_image: Image, dims: Tuple[int, int]) -> Tuple[
hist_g = g.histogram()
hist_b = b.histogram()

noise_r = random.choice(256, p=np.divide(np.copy(hist_r), np.sum(hist_r)), size=size)
noise_g = random.choice(256, p=np.divide(np.copy(hist_g), np.sum(hist_g)), size=size)
noise_b = random.choice(256, p=np.divide(np.copy(hist_b), np.sum(hist_b)), size=size)
noise_r = random.choice(256, p=np.divide(
np.copy(hist_r), np.sum(hist_r)), size=size)
noise_g = random.choice(256, p=np.divide(
np.copy(hist_g), np.sum(hist_g)), size=size)
noise_b = random.choice(256, p=np.divide(
np.copy(hist_b), np.sum(hist_b)), size=size)

noise = Image.new('RGB', (width, height))

Expand All @@ -47,10 +101,16 @@ def blend_source_histogram(source_image: Image, dims: Tuple[int, int]) -> Tuple[
return noise



# based on https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/master/scripts/outpainting_mk_2.py#L175-L232
def expand_image(source_image: Image, mask_image: Image, dims: Tuple[int, int, int, int], fill = 'white', blend_source=blend_source_histogram, blend_op=blend_mask_inverse_source):
left, right, top, bottom = dims
def expand_image(
source_image: Image,
mask_image: Image,
expand_by: Tuple[int, int, int, int],
fill='white',
noise_source=noise_source_histogram,
blend_op=blend_mask_inverse_source
):
left, right, top, bottom = expand_by

full_width = left + source_image.width + right
full_height = top + source_image.height + bottom
Expand All @@ -61,7 +121,7 @@ def expand_image(source_image: Image, mask_image: Image, dims: Tuple[int, int, i
full_mask = Image.new('RGB', (full_width, full_height), fill)
full_mask.paste(mask_image, (left, top))

full_noise = blend_source(source_image, (full_width, full_height))
full_noise = noise_source(source_image, (full_width, full_height))

for x in range(full_source.width):
for y in range(full_source.height):
Expand All @@ -70,6 +130,7 @@ def expand_image(source_image: Image, mask_image: Image, dims: Tuple[int, int, i
source_color = full_source.getpixel((x, y))

if mask_color[0] > 0:
full_source.putpixel((x, y), blend_op(source_color, mask_color, noise_color))
full_source.putpixel((x, y), blend_op(
source_color, mask_color, noise_color))

return (full_source, full_mask, full_noise, (full_width, full_height))

0 comments on commit 9376de8

Please sign in to comment.