2023-02-05 13:53:26 +00:00
|
|
|
import numpy as np
|
2023-01-14 21:19:41 +00:00
|
|
|
from numpy import random
|
2023-04-14 01:06:33 +00:00
|
|
|
from PIL import Image, ImageFilter
|
2023-01-14 21:19:41 +00:00
|
|
|
|
2023-04-14 02:10:00 +00:00
|
|
|
from ..params import Point
|
2023-01-15 03:46:14 +00:00
|
|
|
|
2023-01-16 01:14:58 +00:00
|
|
|
|
2023-01-16 13:49:25 +00:00
|
|
|
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(
|
2023-02-18 22:35:57 +00:00
|
|
|
source: Image.Image, dims: Point, origin: Point, fill="white", **kw
|
2023-02-05 13:53:26 +00:00
|
|
|
) -> Image.Image:
|
|
|
|
"""
|
2023-01-15 15:21:09 +00:00
|
|
|
Identity transform, source image centered on white canvas.
|
2023-02-05 13:53:26 +00:00
|
|
|
"""
|
2023-01-15 06:14:05 +00:00
|
|
|
width, height = dims
|
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
noise = Image.new(source.mode, (width, height), fill)
|
2023-02-18 22:35:57 +00:00
|
|
|
noise.paste(source, origin)
|
2023-01-15 06:14:05 +00:00
|
|
|
|
|
|
|
return noise
|
|
|
|
|
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
def noise_source_fill_mask(
|
2023-11-26 00:52:47 +00:00
|
|
|
source: Image.Image, dims: Point, _origin: Point, fill="white", **kw
|
2023-02-05 13:53:26 +00:00
|
|
|
) -> Image.Image:
|
|
|
|
"""
|
2023-01-15 20:26:04 +00:00
|
|
|
Fill the whole canvas, no source or noise.
|
2023-02-05 13:53:26 +00:00
|
|
|
"""
|
2023-01-15 20:26:04 +00:00
|
|
|
width, height = dims
|
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
noise = Image.new(source.mode, (width, height), fill)
|
2023-01-15 20:26:04 +00:00
|
|
|
|
|
|
|
return noise
|
|
|
|
|
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
def noise_source_gaussian(
|
2023-02-18 22:35:57 +00:00
|
|
|
source: Image.Image, dims: Point, origin: Point, rounds=3, **kw
|
2023-02-05 13:53:26 +00:00
|
|
|
) -> Image.Image:
|
|
|
|
"""
|
2023-01-15 15:21:09 +00:00
|
|
|
Gaussian blur, source image centered on white canvas.
|
2023-02-05 13:53:26 +00:00
|
|
|
"""
|
2023-02-18 22:35:57 +00:00
|
|
|
noise = noise_source_uniform(source, dims, origin)
|
|
|
|
noise.paste(source, origin)
|
2023-01-15 15:21:09 +00:00
|
|
|
|
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))
|
2023-01-15 06:14:05 +00:00
|
|
|
|
|
|
|
return noise
|
|
|
|
|
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
def noise_source_uniform(
|
2023-11-26 00:52:47 +00:00
|
|
|
source: Image.Image, dims: Point, _origin: Point, **kw
|
2023-02-05 13:53:26 +00:00
|
|
|
) -> Image.Image:
|
2023-01-15 03:46:14 +00:00
|
|
|
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)
|
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
# needs to be RGB for pixel manipulation
|
2023-02-05 13:53:26 +00:00
|
|
|
noise = Image.new("RGB", (width, height))
|
2023-01-15 03:46:14 +00:00
|
|
|
|
|
|
|
for x in range(width):
|
|
|
|
for y in range(height):
|
2023-01-16 13:49:25 +00:00
|
|
|
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])))
|
2023-01-15 03:46:14 +00:00
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
return noise.convert(source.mode)
|
2023-01-15 03:46:14 +00:00
|
|
|
|
|
|
|
|
2023-02-05 13:53:26 +00:00
|
|
|
def noise_source_normal(
|
2023-11-26 00:52:47 +00:00
|
|
|
source: Image.Image, dims: Point, _origin: Point, **kw
|
2023-02-05 13:53:26 +00:00
|
|
|
) -> Image.Image:
|
2023-01-15 03:46:14 +00:00
|
|
|
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)
|
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
# needs to be RGB for pixel manipulation
|
2023-02-05 13:53:26 +00:00
|
|
|
noise = Image.new("RGB", (width, height))
|
2023-01-15 03:46:14 +00:00
|
|
|
|
|
|
|
for x in range(width):
|
|
|
|
for y in range(height):
|
2023-01-16 13:49:25 +00:00
|
|
|
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])))
|
2023-01-15 03:46:14 +00:00
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
return noise.convert(source.mode)
|
2023-01-15 03:46:14 +00:00
|
|
|
|
|
|
|
|
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:
|
2023-11-25 13:50:54 +00:00
|
|
|
r, g, b, *_a = source.split()
|
2023-01-14 21:19:41 +00:00
|
|
|
width, height = dims
|
2023-01-14 21:44:19 +00:00
|
|
|
size = width * height
|
2023-01-14 21:19:41 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2023-01-14 21:19:41 +00:00
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
# 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):
|
2023-01-16 13:49:25 +00:00
|
|
|
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]))
|
2023-01-14 21:19:41 +00:00
|
|
|
|
2023-11-26 00:52:47 +00:00
|
|
|
return noise.convert(source.mode)
|