Remove editor module
This commit is contained in:
parent
e5959268c1
commit
c966866abc
37 changed files with 7752 additions and 7345 deletions
5
src/operations/__init__.py
Normal file
5
src/operations/__init__.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from .add_noise import add_noise
|
||||
from .add_random_colour_spill import add_random_colour_spill
|
||||
from .gamma import adjust_gamma, get_random_gamma
|
||||
from .get_colour_lut import get_random_saturation_per_hue_lut, get_random_brightness_lut
|
||||
from .apply_pixel_shader import apply_pixel_shader
|
||||
10
src/operations/add_noise.py
Normal file
10
src/operations/add_noise.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def add_noise(img: Image, alpha: float) -> Image:
|
||||
width, height = img.size
|
||||
random_colors = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8)
|
||||
random_img = Image.fromarray(random_colors)
|
||||
result = Image.blend(img, random_img, alpha)
|
||||
return result
|
||||
20
src/operations/add_random_colour_spill.py
Normal file
20
src/operations/add_random_colour_spill.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
from PIL import Image
|
||||
from utils import random
|
||||
|
||||
|
||||
def add_random_colour_spill(image: Image, range: float) -> Image:
|
||||
matrix = (
|
||||
random(1 - range, 1 + range),
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
random(1 - range, 1 + range),
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
0.0,
|
||||
random(1 - range, 1 + range),
|
||||
0.0,
|
||||
)
|
||||
return image.convert("RGB", matrix)
|
||||
14
src/operations/apply_pixel_shader.py
Normal file
14
src/operations/apply_pixel_shader.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from typing import Callable, Tuple
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def apply_pixel_shader(
|
||||
img: Image, callback: Callable[[int, int, int], Tuple[int, int, int]]
|
||||
):
|
||||
width, height = img.size
|
||||
pixels = img.load()
|
||||
for x in range(width):
|
||||
for y in range(height):
|
||||
r, g, b = pixels[x, y]
|
||||
pixels[x, y] = callback(r, g, b)
|
||||
return img
|
||||
58
src/operations/gamma.ipynb
Normal file
58
src/operations/gamma.ipynb
Normal file
File diff suppressed because one or more lines are too long
12
src/operations/gamma.py
Normal file
12
src/operations/gamma.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import numpy as np
|
||||
from random import choice
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def adjust_gamma(img: Image, gamma: float) -> Image:
|
||||
return img.point(lambda x: (x / 255) ** gamma * 255)
|
||||
|
||||
|
||||
def get_random_gamma() -> float:
|
||||
gamma = np.random.beta(1, 2) * 0.6
|
||||
return 1 / (1 + gamma) if choice([True, False]) else (gamma + 1)
|
||||
93
src/operations/get_colour_lut.ipynb
Normal file
93
src/operations/get_colour_lut.ipynb
Normal file
File diff suppressed because one or more lines are too long
43
src/operations/get_colour_lut.py
Normal file
43
src/operations/get_colour_lut.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import numpy as np
|
||||
from typing import List
|
||||
from utils.random import random
|
||||
from .interpolate import interpolate, INTERPOLATION_TYPE
|
||||
|
||||
|
||||
def get_random_saturation_per_hue_lut(
|
||||
variance: float = 0.4, count: int = 12
|
||||
) -> List[int]:
|
||||
edit_points = [random(-variance, variance) + 1 for _ in range(count)]
|
||||
|
||||
return [
|
||||
interpolate(edit_points, i / 255, type="cubic")
|
||||
for i in np.linspace(0, 255, 256)
|
||||
]
|
||||
|
||||
|
||||
def get_random_brightness_lut(
|
||||
variance: float = 0.1,
|
||||
count: int = 6,
|
||||
type: INTERPOLATION_TYPE = "linear",
|
||||
min_spectrum_size: float = 0.7,
|
||||
max_spectrum_size: float = 1.1,
|
||||
) -> List[int]:
|
||||
spectrum_size = np.random.uniform(min_spectrum_size, max_spectrum_size)
|
||||
spectrum_start = np.random.uniform(0, max(0, 1 - spectrum_size))
|
||||
edit_points = sorted(
|
||||
[
|
||||
spectrum_start,
|
||||
*[
|
||||
spectrum_start
|
||||
+ i * spectrum_size / (count - 1)
|
||||
+ np.random.uniform(-variance, variance)
|
||||
for i in range(1, count - 1)
|
||||
],
|
||||
spectrum_start + spectrum_size,
|
||||
]
|
||||
)
|
||||
|
||||
return [
|
||||
round(interpolate(edit_points, i / 255, type=type) * 255)
|
||||
for i in np.linspace(0, 255, 256)
|
||||
]
|
||||
33
src/operations/interpolate.py
Normal file
33
src/operations/interpolate.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import numpy as np
|
||||
from scipy.interpolate import CubicSpline
|
||||
from typing import List, Literal
|
||||
|
||||
|
||||
INTERPOLATION_TYPE = Literal["cubic", "linear"]
|
||||
|
||||
|
||||
def interpolate(
|
||||
control_points: List[float], t: float, type: INTERPOLATION_TYPE
|
||||
) -> float:
|
||||
if type == "cubic":
|
||||
x = np.linspace(0, 1, len(control_points))
|
||||
cs = CubicSpline(x, control_points)
|
||||
return cs(t)
|
||||
|
||||
if type == "linear":
|
||||
n = len(control_points) - 1
|
||||
segment_indices = np.linspace(0, 1, n + 1)
|
||||
|
||||
index = np.searchsorted(segment_indices, t, side="right") - 1
|
||||
|
||||
if t == 1:
|
||||
return control_points[-1]
|
||||
else:
|
||||
t_normalized = (t - segment_indices[index]) / (
|
||||
segment_indices[index + 1] - segment_indices[index]
|
||||
)
|
||||
return control_points[index] + t_normalized * (
|
||||
control_points[index + 1] - control_points[index]
|
||||
)
|
||||
|
||||
raise ValueError("Invalid type")
|
||||
Loading…
Add table
Add a link
Reference in a new issue