replaces dither and kmeans quantize with quantize

much faster dithering and uses PIL
This commit is contained in:
EllangoK
2023-04-03 15:49:06 -04:00
parent 08a83e3d51
commit 99956683a0
6 changed files with 133 additions and 120 deletions
-63
View File
@@ -1,63 +0,0 @@
import torch
class Dither:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"bits": ("INT", {
"default": 4,
"min": 1,
"max": 8,
"step": 1
}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "dither"
CATEGORY = "postprocessing"
def dither(self, image: torch.Tensor, bits: int):
batch_size, height, width, _ = image.shape
result = torch.zeros_like(image)
for b in range(batch_size):
tensor_image = image[b]
img = (tensor_image * 255)
height, width, _ = img.shape
scale = 255 / (2**bits - 1)
for y in range(height):
for x in range(width):
old_pixel = img[y, x].clone()
new_pixel = torch.round(old_pixel / scale) * scale
img[y, x] = new_pixel
quant_error = old_pixel - new_pixel
if x + 1 < width:
img[y, x + 1] += quant_error * 7 / 16
if y + 1 < height:
if x - 1 >= 0:
img[y + 1, x - 1] += quant_error * 3 / 16
img[y + 1, x] += quant_error * 5 / 16
if x + 1 < width:
img[y + 1, x + 1] += quant_error * 1 / 16
dithered = img / 255
tensor = dithered.unsqueeze(0)
result[b] = tensor
return (result,)
NODE_CLASS_MAPPINGS = {
"Dither": Dither
}
-64
View File
@@ -1,64 +0,0 @@
import cv2
import numpy as np
import torch
class KMeansQuantize:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"colors": ("INT", {
"default": 16,
"min": 1,
"max": 256,
"step": 1
}),
"precision": ("INT", {
"default": 10,
"min": 1,
"max": 100,
"step": 1
}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "kmeans_quantize"
CATEGORY = "postprocessing"
def kmeans_quantize(self, image: torch.Tensor, colors: int, precision: int):
batch_size, height, width, _ = image.shape
result = torch.zeros_like(image)
for b in range(batch_size):
tensor_image = image[b].numpy().astype(np.float32)
img = tensor_image
height, width, c = img.shape
criteria = (
cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,
precision * 5, 0.01
)
img_copy = img.reshape(-1, c)
_, label, center = cv2.kmeans(
img_copy, colors, None,
criteria, 1, cv2.KMEANS_PP_CENTERS
)
img = center[label.flatten()].reshape(*img.shape)
tensor = torch.from_numpy(img).unsqueeze(0)
result[b] = tensor
return (result,)
NODE_CLASS_MAPPINGS = {
"KMeansQuantize": KMeansQuantize
}
+50
View File
@@ -0,0 +1,50 @@
import torch
from PIL import Image
import numpy as np
class Quantize:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"colors": ("INT", {
"default": 256,
"min": 1,
"max": 256,
"step": 1
}),
"dither": (["none", "floyd-steinberg"],),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "quantize"
CATEGORY = "postprocessing"
def quantize(self, image: torch.Tensor, colors: int = 256, dither: str = "FLOYDSTEINBERG"):
batch_size, height, width, _ = image.shape
result = torch.zeros_like(image)
dither_option = Image.Dither.FLOYDSTEINBERG if dither == "floyd-steinberg" else Image.Dither.NONE
for b in range(batch_size):
tensor_image = image[b]
img = (tensor_image * 255).to(torch.uint8).numpy()
pil_image = Image.fromarray(img, mode='RGB')
palette = pil_image.quantize(colors=colors) # Required as described in https://github.com/python-pillow/Pillow/issues/5836
quantized_image = pil_image.quantize(colors=colors, palette=palette, dither=dither_option)
quantized_array = torch.tensor(np.array(quantized_image.convert("RGB"))).float() / 255
result[b] = quantized_array
return (result,)
NODE_CLASS_MAPPINGS = {
"Quantize": Quantize,
}