-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create pixel_blur.py Add pixel blur node similar to "Pixelize" in gimp. * Update pixel_blur.py * Update pixel_blur.py * Update pixel_blur.py added padding * Update pixel_blur.py fix greyscale image * change name & category --------- Co-authored-by: Joey Ballentine <34788790+joeyballentine@users.noreply.github.com> Co-authored-by: Michael Schmidt <mitchi5000.ms@googlemail.com>
- Loading branch information
1 parent
5b1e891
commit f5cb007
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
backend/src/packages/chaiNNer_standard/image_filter/miscellaneous/pixelate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from __future__ import annotations | ||
|
||
import cv2 | ||
import numpy as np | ||
|
||
from nodes.properties.inputs import ImageInput, NumberInput | ||
from nodes.properties.outputs import ImageOutput | ||
|
||
from .. import miscellaneous_group | ||
|
||
|
||
@miscellaneous_group.register( | ||
schema_id="chainner:image:pixelate", | ||
name="Pixelate", | ||
description="Pixelate an image.", | ||
icon="MdOutlineAutoFixHigh", | ||
inputs=[ | ||
ImageInput(), | ||
NumberInput("Size X", default=10, precision=0, minimum=1, controls_step=1), | ||
NumberInput("Size Y", default=10, precision=0, minimum=1, controls_step=1), | ||
], | ||
outputs=[ImageOutput(image_type="Input0")], | ||
) | ||
def pixelate_node( | ||
img: np.ndarray, | ||
size_x: int, | ||
size_y: int, | ||
) -> np.ndarray: | ||
block_sizes = (size_x, size_y) | ||
height, width = img.shape[:2] | ||
|
||
pad_x = (block_sizes[0] - width % block_sizes[0]) % block_sizes[0] | ||
pad_y = (block_sizes[1] - height % block_sizes[1]) % block_sizes[1] | ||
img = cv2.copyMakeBorder(img, 0, pad_y, 0, pad_x, cv2.BORDER_REFLECT_101) | ||
|
||
num_blocks_x = width // block_sizes[0] | ||
num_blocks_y = height // block_sizes[1] | ||
|
||
blocks = img[: num_blocks_y * block_sizes[1], : num_blocks_x * block_sizes[0]] | ||
blocks = ( | ||
blocks.reshape( | ||
(num_blocks_y, block_sizes[1], num_blocks_x, block_sizes[0], img.shape[-1]) | ||
) | ||
if len(img.shape) > 2 | ||
else blocks.reshape( | ||
(num_blocks_y, block_sizes[1], num_blocks_x, block_sizes[0]) | ||
) | ||
) | ||
|
||
average_colors = np.mean(np.mean(blocks, axis=1), axis=2) | ||
|
||
repeated_colors = np.repeat( | ||
np.repeat(average_colors, block_sizes[0], axis=1), block_sizes[1], axis=0 | ||
) | ||
|
||
return repeated_colors[:height, :width] |