diff --git a/ctif.py b/ctif.py index db3f1d6..751f280 100644 --- a/ctif.py +++ b/ctif.py @@ -42,7 +42,9 @@ CHARS = list(reversed([ ' ' ])) -NEW_CHARS = '.:\',`;_<^~%>"?!ir=clsxzL}ovCS(+eknuFJfj{amwPZtIT)*hbdpqyEGKOVXYgU&ABDHQR[]MNW@$#' +NEW_CHARS = ' .:\',`;_<^~%>"?!ir=clsxzL}ovCS(+eknuFJfj{amwPZtIT)*hbdpqyEGKOVXYgU&ABDHQR[]MNW@$#' + +EDGE_CHARS = '-\\|/' ALL_CHARS = [ u'█', @@ -78,6 +80,8 @@ class CTIF: self.convert(image) def convert(self, image: Image.Image) -> None: + if image.mode != 'RGB': + image = image.convert('RGB') image = image.resize((image.width * RESIZE_FACTOR, image.height * RESIZE_FACTOR)) logging.debug(f'Image Size: {image.size}') @@ -90,7 +94,7 @@ class CTIF: dog.save('debug/dog.png') - sb, gradient = filters.sobel(dog, 0.8) + sb, gradient = filters.sobel(dog) # image = image.convert('HSV') w, h = sb.size @@ -138,8 +142,9 @@ class CTIF: else: histogram[char] = 1 - color_avg = filters.average_colors(color) - self.colors.append(color_avg) + r, g, b = filters.average_colors(color) + color_avg_hsv = colorsys.rgb_to_hsv(r/255, g/255, b/255) + self.colors.append(color_avg_hsv) # get most common most_common = None @@ -191,6 +196,7 @@ class CTIF: print() def save_image(self, path: str) -> None: + logging.debug('Rendering and Saving image...') pg.init() font = pg.font.Font(FONT_PATH, 12) @@ -199,12 +205,24 @@ 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): - r, g, b = color - h, s, v = colorsys.rgb_to_hsv(r, g, b) - new_color = colorsys.hsv_to_rgb(h, s, 192) + h, s, v = color + if char in EDGE_CHARS: + v = 255 + else: + v = (NEW_CHARS.find(char) + 1) * (255/len(NEW_CHARS)) - # print(color) - rendered_char = font.render(char, ANTI_ALIASING, new_color) + logging.debug(f'HSV Color: {h, s, v/255}') + r, g, b = colorsys.hsv_to_rgb(h, s, v/255) + + + logging.debug(f'RGB Color Before: {r, g, b}') + r *= 255 + g *= 255 + + r = min(r, 255) + g = min(g, 255) + logging.debug(f'RGB Color: {r, g, b}') + rendered_char = font.render(char, ANTI_ALIASING, (int(r), int(g), int(b))) window.blit(rendered_char, (x * TEXT_WIDTH, y * TEXT_HEIGHT)) x += 1 @@ -227,11 +245,10 @@ class CTIF: logging.debug(f'Rendering CITF Image with Dim: ({self.width}, {self.height})') for (color, char) in zip(self.colors, self.text): - if char in ('/', '\\', '|', '-'): + if char in EDGE_CHARS: - r, g, b = color - h, s, v = colorsys.rgb_to_hsv(r, g, b) - new_color = colorsys.hsv_to_rgb(h, s, 192) + h, s, v = color + new_color = colorsys.hsv_to_rgb(h, s, 255) # print(color) rendered_char = font.render(char, ANTI_ALIASING, new_color) @@ -248,17 +265,61 @@ class CTIF: pg.quit() def save_citf(self, filename: str) -> None: - # 3 byte for color, byte for char + # 1 byte for h, 1 byte for s, 1 byte for character which will be interperated as v when rendering - b = bytes() + bin = bytes() + + # Save Width and Height + bin += self.width.to_bytes(2, 'big') + bin += self.height.to_bytes(2, 'big') for (color, char) in zip(self.colors, self.text): - for n in color: - b += n.to_bytes(1, 'big') + h, s, v = color - b += ALL_CHARS.index(char).to_bytes(1, 'big') + h = int((h * 360) / (360/255)) + bin += h.to_bytes(1, 'big') + + s = int((s*100) / (100/255)) + bin += s.to_bytes(1, 'big') + + # for n in (h, s): + # i = int(n * 255) + + # bin += i.to_bytes(1, 'big') + + bin += (NEW_CHARS + EDGE_CHARS).index(char).to_bytes(1, 'big') with open(f'{filename}.citf', 'wb+') as file: - file.write(b) + file.write(bin) + def read_citf(self, filepath: str) -> None: + self.text = '' + self.colors = [] + + with open(filepath, 'rb') as file: + data = file.read() + + # Get Width and Height + self.width = int.from_bytes(data[:2], 'big') + self.height = int.from_bytes(data[2:4], 'big') + + data = data[4:] + + # Read colors and characters + char = None + h, s = None, None + for n in data: + if not h: + h = n + elif not s: + s = n + else: + print(n) + char = (NEW_CHARS + EDGE_CHARS)[n] + + if h and s and char: + logging.debug(f'H: {h}, S: {s}, Char: {char}') + self.text += char + self.colors.append((h/255, s/255, 255)) + h, s, char = None, None, None diff --git a/engine.citf b/engine.citf index c9eb201..9804615 100644 Binary files a/engine.citf and b/engine.citf differ diff --git a/main.py b/main.py index 50967d4..9fa005e 100644 --- a/main.py +++ b/main.py @@ -13,8 +13,11 @@ def main(): image = Image.open(IMAGE_PATH) i = ctif.CTIF(image) - i.save_edges('debug/edges-render.png') + i.save_citf('engine') + i.read_citf('engine.citf') i.save_image('render.png') + # i.save_edges('debug/edges-render.png') + # i.save_image('render.png') # i.save_citf('engine') if __name__ == '__main__':