1
0
Fork 0
onnx-web/api/onnx_web/image/noise_source.py

126 lines
3.3 KiB
Python
Raw Normal View History

2023-02-05 13:53:26 +00:00
import numpy as np
from numpy import random
from PIL import Image, ImageFilter
from ..params import Point
def get_pixel_index(x: int, y: int, width: int) -> int:
return (y * width) + x
2023-02-05 13:53:26 +00:00
def noise_source_fill_edge(
source: Image.Image, dims: Point, origin: Point, fill="white", **kw
2023-02-05 13:53:26 +00:00
) -> Image.Image:
"""
Identity transform, source image centered on white canvas.
2023-02-05 13:53:26 +00:00
"""
width, height = dims
noise = Image.new(source.mode, (width, height), fill)
noise.paste(source, origin)
return noise
2023-02-05 13:53:26 +00:00
def noise_source_fill_mask(
source: Image.Image, dims: Point, _origin: Point, fill="white", **kw
2023-02-05 13:53:26 +00:00
) -> Image.Image:
"""
Fill the whole canvas, no source or noise.
2023-02-05 13:53:26 +00:00
"""
width, height = dims
noise = Image.new(source.mode, (width, height), fill)
return noise
2023-02-05 13:53:26 +00:00
def noise_source_gaussian(
source: Image.Image, dims: Point, origin: Point, rounds=3, **kw
2023-02-05 13:53:26 +00:00
) -> Image.Image:
"""
Gaussian blur, source image centered on white canvas.
2023-02-05 13:53:26 +00:00
"""
noise = noise_source_uniform(source, dims, origin)
noise.paste(source, origin)
2023-02-20 04:10:35 +00:00
for _i in range(rounds):
2023-01-15 16:54:17 +00:00
noise = noise.filter(ImageFilter.GaussianBlur(5))
return noise
2023-02-05 13:53:26 +00:00
def noise_source_uniform(
source: Image.Image, dims: Point, _origin: Point, **kw
2023-02-05 13:53:26 +00:00
) -> Image.Image:
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)
# needs to be RGB for pixel manipulation
2023-02-05 13:53:26 +00:00
noise = Image.new("RGB", (width, height))
for x in range(width):
for y in range(height):
i = get_pixel_index(x, y, width)
2023-02-05 13:53:26 +00:00
noise.putpixel((x, y), (int(noise_r[i]), int(noise_g[i]), int(noise_b[i])))
return noise.convert(source.mode)
2023-02-05 13:53:26 +00:00
def noise_source_normal(
source: Image.Image, dims: Point, _origin: Point, **kw
2023-02-05 13:53:26 +00:00
) -> Image.Image:
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)
# needs to be RGB for pixel manipulation
2023-02-05 13:53:26 +00:00
noise = Image.new("RGB", (width, height))
for x in range(width):
for y in range(height):
i = get_pixel_index(x, y, width)
2023-02-05 13:53:26 +00:00
noise.putpixel((x, y), (int(noise_r[i]), int(noise_g[i]), int(noise_b[i])))
return noise.convert(source.mode)
2023-02-05 13:53:26 +00:00
def noise_source_histogram(
2023-02-19 13:54:27 +00:00
source: Image.Image, dims: Point, _origin: Point, **kw
2023-02-05 13:53:26 +00:00
) -> Image.Image:
r, g, b, *_a = source.split()
width, height = dims
2023-01-14 21:44:19 +00:00
size = width * height
hist_r = r.histogram()
hist_g = g.histogram()
hist_b = b.histogram()
2023-02-05 13:53:26 +00:00
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
)
# needs to be RGB for pixel manipulation
2023-02-05 13:53:26 +00:00
noise = Image.new("RGB", (width, height))
2023-01-14 22:14:37 +00:00
for x in range(width):
for y in range(height):
i = get_pixel_index(x, y, width)
2023-02-05 13:53:26 +00:00
noise.putpixel((x, y), (noise_r[i], noise_g[i], noise_b[i]))
return noise.convert(source.mode)