working on a new color algo

This commit is contained in:
JISAUAY 2024-09-12 13:30:54 -05:00
parent af68d788b0
commit cb0fa7b15d
4 changed files with 74 additions and 9 deletions

59
brightness.py Normal file
View File

@ -0,0 +1,59 @@
import string
from PIL import Image
import pygame as pg
FONT_PATH = 'fonts/scientifica.ttf'
CHARS = (string.ascii_letters + string.punctuation).replace('\\', '').replace('/', '').replace('|', '').replace('-', '')
sorted_chars = {}
for c in CHARS:
pg.init()
font = pg.font.Font(FONT_PATH, 12)
font_w, font_h = font.size(c)
window = pg.display.set_mode((font_w, font_h))
rendered_char = font.render(c, False, (255, 255, 255))
window.blit(rendered_char, (0, 0))
pg.display.update()
pg.image.save(window, 'temp.png')
pg.quit()
image = Image.open('temp.png')
w, h = image.size
white = 0
for pixel_y in range(h):
for pixel_x in range(w):
pixel = image.getpixel((pixel_x, pixel_y))
if pixel == (255, 255, 255):
white += 1
sorted_chars[c] = white/(w * h)
sorted_chars = dict(sorted(sorted_chars.items(), key=lambda item: item[1]))
chr_str = ''
for k, v in sorted_chars.items():
chr_str += k
print(chr_str)

20
ctif.py
View File

@ -40,6 +40,8 @@ CHARS = list(reversed([
' '
]))
NEW_CHARS = '.:\',`;_<^~%>"?!ir=clsxzL}ovCS(+eknuFJfj{amwPZtIT)*hbdpqyEGKOVXYgU&ABDHQR[]MNW@$#'
ALL_CHARS = [
u'',
'@',
@ -87,15 +89,16 @@ class CTIF:
dog.save('debug/dog.png')
sb, gradient = filters.sobel(dog)
image = image.convert('HSV')
w, h = sb.size
sb.save('debug/sobel.png')
text_grid_width = w / SCALED_WIDTH
text_grid_width = w // SCALED_WIDTH
self.width = text_grid_width
text_grind_height = h / SCALED_HEIGHT
text_grind_height = h // SCALED_HEIGHT
self.height = text_grind_height
y_offset = 0
@ -110,10 +113,14 @@ class CTIF:
for y2 in range(SCALED_HEIGHT):
for x2 in range(SCALED_WIDTH):
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)
gradient_v = gradient[real_y * w + real_x]
char = ' '
@ -121,15 +128,13 @@ class CTIF:
if gradient_v:
char = self._match_gradient(gradient_v)
else:
char = CHARS[round(L_image.getpixel((real_x, real_y))/(255/len(CHARS))) - 1]
char = NEW_CHARS[round(v/(255/len(NEW_CHARS))) - 1]
if char in histogram:
histogram[char] += 1
else:
histogram[char] = 1
color.append(image.getpixel((real_x, real_y)))
color_avg = filters.average_colors(color)
self.colors.append(color_avg)
@ -191,13 +196,14 @@ class CTIF:
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):
rendered_char = font.render(char, True, color)
window.blit(rendered_char, (x * TEXT_WIDTH, y * TEXT_HEIGHT))
x += 1
if x == self.width:
if x >= self.width:
x = 0
y += 1

BIN
engine.citf Normal file

Binary file not shown.

View File

@ -7,13 +7,13 @@ import ctif
FORMAT = "%(levelname)s %(filename)s - %(message)s"
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
IMAGE_PATH = 'sample-images/engine.PNG'
IMAGE_PATH = 'sample-images/sunflower.jpg'
def main():
image = Image.open(IMAGE_PATH)
i = ctif.CTIF(image)
# i.save_image('render.png')
i.save_image('render.png')
i.save_citf('engine')
if __name__ == '__main__':