Skip to content

Instantly share code, notes, and snippets.

@phuang1024
Created October 28, 2023 03:15
Show Gist options
  • Select an option

  • Save phuang1024/fe96271ba9f69317d963b6e1afc838d0 to your computer and use it in GitHub Desktop.

Select an option

Save phuang1024/fe96271ba9f69317d963b6e1afc838d0 to your computer and use it in GitHub Desktop.
Create large ascii text.
#!/usr/bin/env python
import argparse
import os
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "1"
import cv2
import numpy as np
import pygame
pygame.init()
parser = argparse.ArgumentParser()
parser.add_argument("text", help="Text to convert.")
parser.add_argument("--font", help="Font to use.", default="ubuntu-condensed", type=str)
parser.add_argument("--size", help="Text height in chars.", default=12, type=int)
parser.add_argument("--aspect", help="Term char aspect.", default=2, type=float)
parser.add_argument("--char", help="Char to use for color.", default="@", type=str)
args = parser.parse_args()
font = pygame.font.SysFont(args.font, args.size * 10)
text_image = font.render(args.text, True, (255, 255, 255))
image = pygame.Surface(text_image.get_size())
image.fill((0, 0, 0))
image.blit(text_image, (0, 0))
image = pygame.surfarray.array3d(image).swapaxes(0, 1)
image = np.average(image, axis=2)
out_height = args.size
out_width = int(out_height * args.aspect * image.shape[1] / image.shape[0])
image = cv2.resize(image, (out_width, out_height), cv2.INTER_AREA)
for line in image:
string = ""
for ch in line:
string += args.char if ch > 127 else " "
print(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment