hsv is one of the color spaces
This commit is contained in:
parent
cb0fa7b15d
commit
8f37552ba6
54
ctif.py
54
ctif.py
@ -1,9 +1,10 @@
|
||||
import colorsys
|
||||
import logging
|
||||
import math
|
||||
import os
|
||||
|
||||
from rich.console import Console
|
||||
from PIL import Image
|
||||
from PIL import Image, ImageFilter
|
||||
import pygame as pg
|
||||
|
||||
import filters
|
||||
@ -22,6 +23,7 @@ SCALED_WIDTH = TEXT_WIDTH // SCALE_FACTOR
|
||||
SCALED_HEIGHT = TEXT_HEIGHT // SCALE_FACTOR
|
||||
|
||||
DEBUG = True
|
||||
ANTI_ALIASING = False
|
||||
|
||||
if not os.path.exists('./debug/') and DEBUG:
|
||||
os.makedirs('./debug')
|
||||
@ -88,8 +90,8 @@ class CTIF:
|
||||
|
||||
dog.save('debug/dog.png')
|
||||
|
||||
sb, gradient = filters.sobel(dog)
|
||||
image = image.convert('HSV')
|
||||
sb, gradient = filters.sobel(dog, 0.8)
|
||||
# image = image.convert('HSV')
|
||||
|
||||
w, h = sb.size
|
||||
|
||||
@ -116,9 +118,10 @@ class CTIF:
|
||||
real_x = x2 + x_offset
|
||||
real_y = y2 + y_offset
|
||||
|
||||
h, s, v = image.getpixel((real_x, real_y))
|
||||
color.append((h, s, v))
|
||||
print(h, s, v)
|
||||
r, g, b = image.getpixel((real_x, real_y))
|
||||
color.append((r, g, b))
|
||||
h, s, v = colorsys.rgb_to_hsv(r, g, b)
|
||||
# print(h, s, v)
|
||||
|
||||
|
||||
gradient_v = gradient[real_y * w + real_x]
|
||||
@ -152,8 +155,6 @@ class CTIF:
|
||||
|
||||
y_offset += SCALED_HEIGHT
|
||||
|
||||
|
||||
|
||||
# TODO @0x01FE : refactor plz increment by 30 & 60
|
||||
def _match_gradient(self, n: float) -> str:
|
||||
|
||||
@ -198,7 +199,12 @@ class CTIF:
|
||||
x, y = 0, 0
|
||||
logging.debug(f'Rendering CITF Image with Dim: ({self.width}, {self.height})')
|
||||
for (color, char) in zip(self.colors, self.text):
|
||||
rendered_char = font.render(char, True, color)
|
||||
r, g, b = color
|
||||
h, s, v = colorsys.rgb_to_hsv(r, g, b)
|
||||
new_color = colorsys.hsv_to_rgb(h, s, 192)
|
||||
|
||||
# print(color)
|
||||
rendered_char = font.render(char, ANTI_ALIASING, new_color)
|
||||
window.blit(rendered_char, (x * TEXT_WIDTH, y * TEXT_HEIGHT))
|
||||
|
||||
x += 1
|
||||
@ -211,6 +217,36 @@ class CTIF:
|
||||
pg.image.save(window, path)
|
||||
pg.quit()
|
||||
|
||||
def save_edges(self, path: str) -> None:
|
||||
pg.init()
|
||||
font = pg.font.Font(FONT_PATH, 12)
|
||||
|
||||
window = pg.display.set_mode((self.width * TEXT_WIDTH, self.height * TEXT_HEIGHT))
|
||||
|
||||
x, y = 0, 0
|
||||
logging.debug(f'Rendering CITF Image with Dim: ({self.width}, {self.height})')
|
||||
for (color, char) in zip(self.colors, self.text):
|
||||
|
||||
if char in ('/', '\\', '|', '-'):
|
||||
|
||||
r, g, b = color
|
||||
h, s, v = colorsys.rgb_to_hsv(r, g, b)
|
||||
new_color = colorsys.hsv_to_rgb(h, s, 192)
|
||||
|
||||
# print(color)
|
||||
rendered_char = font.render(char, ANTI_ALIASING, new_color)
|
||||
window.blit(rendered_char, (x * TEXT_WIDTH, y * TEXT_HEIGHT))
|
||||
|
||||
x += 1
|
||||
|
||||
if x >= self.width:
|
||||
x = 0
|
||||
y += 1
|
||||
|
||||
pg.display.update()
|
||||
pg.image.save(window, path)
|
||||
pg.quit()
|
||||
|
||||
def save_citf(self, filename: str) -> None:
|
||||
# 3 byte for color, byte for char
|
||||
|
||||
|
||||
@ -61,6 +61,9 @@ def sobel(img: Image.Image, gradient_threshold: int | None = 20) -> tuple[Image.
|
||||
logging.debug(f'edges size: {edges.size}')
|
||||
|
||||
gradient = np.arctan2(dy.flatten(), dx.flatten())
|
||||
gradient = np.absolute(gradient)
|
||||
print(gradient)
|
||||
print(np.average(gradient))
|
||||
gradient[gradient > gradient_threshold] = 0
|
||||
|
||||
pil_edges = edges.astype(np.uint8)
|
||||
|
||||
5
main.py
5
main.py
@ -7,14 +7,15 @@ import ctif
|
||||
FORMAT = "%(levelname)s %(filename)s - %(message)s"
|
||||
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
|
||||
|
||||
IMAGE_PATH = 'sample-images/sunflower.jpg'
|
||||
IMAGE_PATH = 'sample-images/engine.PNG'
|
||||
|
||||
def main():
|
||||
|
||||
image = Image.open(IMAGE_PATH)
|
||||
i = ctif.CTIF(image)
|
||||
i.save_edges('debug/edges-render.png')
|
||||
i.save_image('render.png')
|
||||
i.save_citf('engine')
|
||||
# i.save_citf('engine')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user