Move dithering function

pull/21/head
Kohaku-Blueleaf 2023-01-24 02:56:33 +08:00
parent d65fcbc7f9
commit 29bf1be3f8
2 changed files with 31 additions and 23 deletions

View File

@ -0,0 +1,31 @@
from typing import Any
from numpy.typing import NDArray
from itertools import product
import cv2
from PIL import Image
import numpy as np
def dithering(
img: NDArray[Any],
find_new_color
):
d_h, d_w, c = img.shape
new_res = np.array(img, dtype=np.float32)/255
for i, j in product(range(d_h), range(d_w)):
old_val = new_res[i, j].copy()
new_val = find_new_color(old_val)
new_res[i, j] = new_val
err = old_val - new_val
if j < d_w - 1:
new_res[i, j+1] += err * 7/16
if i < d_h - 1:
new_res[i+1, j] += err * 5/16
if j > 0:
new_res[i+1, j-1] += err * 3/16
if j < d_w - 1:
new_res[i+1, j+1] += err * 1/16
return np.clip(new_res/np.max(new_res, axis=(0,1))*255, 0, 255)

View File

@ -81,29 +81,6 @@ def preprocess(
return img
def dithering(
img: NDArray[Any],
find_new_color
):
d_h, d_w, c = img.shape
new_res = np.array(img, dtype=np.float32)/255
for i, j in product(range(d_h), range(d_w)):
old_val = new_res[i, j].copy()
new_val = find_new_color(old_val)
new_res[i, j] = new_val
err = old_val - new_val
if j < d_w - 1:
new_res[i, j+1] += err * 7/16
if i < d_h - 1:
new_res[i+1, j] += err * 5/16
if j > 0:
new_res[i+1, j-1] += err * 3/16
if j < d_w - 1:
new_res[i+1, j+1] += err * 1/16
return np.clip(new_res/np.max(new_res, axis=(0,1))*255, 0, 255)
def pixelize(
img: NDArray[Any],
k: int, c: int,