|
1 | 1 | import torch |
| 2 | +from PIL import Image, ImageDraw, ImageFont |
| 3 | +import numpy as np |
2 | 4 | import torch.nn.functional as F |
3 | 5 | import cv2 |
4 | | -import numpy as np |
5 | 6 | from PIL import Image, ImageEnhance |
6 | 7 | from PIL import Image |
7 | 8 |
|
@@ -47,6 +48,73 @@ def subtract(self, img1, img2): |
47 | 48 | def difference(self, img1, img2): |
48 | 49 | return torch.abs(img1 - img2) |
49 | 50 |
|
| 51 | +class AsciiArt: |
| 52 | + def __init__(self): |
| 53 | + pass |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def INPUT_TYPES(s): |
| 57 | + return { |
| 58 | + "required": { |
| 59 | + "image": ("IMAGE",), |
| 60 | + "char_size": ("INT", { |
| 61 | + "default": 12, |
| 62 | + "min": 0, |
| 63 | + "max": 64, |
| 64 | + "step": 2, |
| 65 | + }), |
| 66 | + "font_size": ("INT", { |
| 67 | + "default": 12, |
| 68 | + "min": 0, |
| 69 | + "max": 64, |
| 70 | + "step": 2, |
| 71 | + }), |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + RETURN_TYPES = ("IMAGE",) |
| 76 | + FUNCTION = "apply_ascii_art_effect" |
| 77 | + |
| 78 | + CATEGORY = "postprocessing/Effects" |
| 79 | + |
| 80 | + def apply_ascii_art_effect(self, image: torch.Tensor, char_size: int, font_size: int): |
| 81 | + batch_size, height, width, channels = image.shape |
| 82 | + result = torch.zeros_like(image) |
| 83 | + |
| 84 | + for b in range(batch_size): |
| 85 | + img_b = image[b] * 255.0 |
| 86 | + img_b = Image.fromarray(img_b.numpy().astype('uint8'), 'RGB') |
| 87 | + result_b = ascii_art_effect(img_b, char_size, font_size) |
| 88 | + result_b = torch.tensor(np.array(result_b)) / 255.0 |
| 89 | + result[b] = result_b |
| 90 | + |
| 91 | + return (result,) |
| 92 | + |
| 93 | + |
| 94 | +def ascii_art_effect(image: torch.Tensor, char_size: int, font_size: int): |
| 95 | + chars = " .'`^\",:;I1!i><-+_-?][}{1)(|\/tfjrxnuvczXYUCLQ0OZmwqpbdkhao*#MW&8%B@$" |
| 96 | + small_image = image.resize((image.size[0] // char_size, image.size[1] // char_size), Image.Resampling.NEAREST) |
| 97 | + |
| 98 | + def get_char(value): |
| 99 | + return chars[value * len(chars) // 256] |
| 100 | + |
| 101 | + ascii_image = Image.new('RGB', image.size, (0, 0, 0)) |
| 102 | + font = ImageFont.truetype("arial.ttf", font_size) |
| 103 | + draw_image = ImageDraw.Draw(ascii_image) |
| 104 | + |
| 105 | + for i in range(small_image.height): |
| 106 | + for j in range(small_image.width): |
| 107 | + r, g, b = small_image.getpixel((j, i)) |
| 108 | + k = (r + g + b) // 3 |
| 109 | + draw_image.text( |
| 110 | + (j * char_size, i * char_size), |
| 111 | + get_char(k), |
| 112 | + font=font, |
| 113 | + fill=(r, g, b) |
| 114 | + ) |
| 115 | + |
| 116 | + return ascii_image |
| 117 | + |
50 | 118 | class Blend: |
51 | 119 | def __init__(self): |
52 | 120 | pass |
@@ -1372,6 +1440,7 @@ def pixel_sort(img, mask, horizontal_sort=False, span_limit=None, sort_by='H', r |
1372 | 1440 |
|
1373 | 1441 | NODE_CLASS_MAPPINGS = { |
1374 | 1442 | "ArithmeticBlend": ArithmeticBlend, |
| 1443 | + "AsciiArt": AsciiArt, |
1375 | 1444 | "Blend": Blend, |
1376 | 1445 | "Blur": Blur, |
1377 | 1446 | "CannyEdgeMask": CannyEdgeMask, |
|
0 commit comments