Skip to content

Instantly share code, notes, and snippets.

@delimitry
Last active October 9, 2024 03:58
Show Gist options
  • Select an option

  • Save delimitry/6a01cbe55657121a1f3d to your computer and use it in GitHub Desktop.

Select an option

Save delimitry/6a01cbe55657121a1f3d to your computer and use it in GitHub Desktop.

Revisions

  1. delimitry revised this gist Dec 21, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion scrolling_text.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # -*- coding: utf-8 -*-
    """
    A tool for scrolling input text as ASCII art text banners in console
    A tool for scrolling input text as ASCII art text banner in console
    """

    import os
  2. delimitry revised this gist Dec 21, 2017. 1 changed file with 65 additions and 52 deletions.
    117 changes: 65 additions & 52 deletions scrolling_text.py
    Original file line number Diff line number Diff line change
    @@ -1,66 +1,79 @@
    # -*- coding: utf-8 -*-
    """
    A tool for scrolling input text as ASCII art text banners in console
    """

    import os
    import time
    from PIL import Image, ImageDraw, ImageFont


    def init_console(cols, lines):
    os.system('mode con: cols=%s lines=%s' % (cols, lines))
    os.system('mode con: cols=%s lines=%s' % (cols, lines))


    def clear_console():
    os.system('cls' if os.name == 'nt' else 'clear')
    os.system('cls' if os.name == 'nt' else 'clear')


    def get_text_size(font, text):
    return font.getsize(text)

    return font.getsize(text)


    def text_to_ascii_text(font, text, cols, lines):
    image = Image.new('RGB',(cols - 1, lines - 1), (255, 255, 255))
    draw = ImageDraw.Draw(image)
    draw.text((0, 0), text, fill='black', font=font)
    width, height = image.size
    pixels = image.load()
    # convert image text pixels to ascii
    out = ''
    for y in xrange(height):
    for x in xrange(width):
    pix = pixels[x, y]
    if pix != (255, 255, 255):
    out += '#'
    else:
    out += ' '
    out += '\n'
    return out
    """Convert text to ASCII art text banner"""
    image = Image.new('RGB', (cols - 1, lines - 1), (255, 255, 255))
    draw = ImageDraw.Draw(image)
    draw.text((0, 0), text, fill='black', font=font)
    width, height = image.size
    pixels = image.load()
    # convert image pixels to ASCII art
    out = ''
    for y in range(height):
    for x in range(width):
    pix = pixels[x, y]
    if pix != (255, 255, 255):
    out += '#'
    else:
    out += ' '
    out += '\n'
    return out


    def main():
    text = 'Scrolling ascii text in console.'

    font = ImageFont.load_default()
    #font = ImageFont.truetype('arial.ttf', 16)

    # get size of space char
    space_width = get_text_size(font, ' ')[0]

    # get size of text
    text_height = get_text_size(font, text)[1]

    # resize console
    cols = 100
    lines = int(text_height * 1.25)
    init_console(cols, lines)

    # add some padding to the text
    padding = ' ' * (cols / space_width + 1)
    text = padding + text

    index = 0
    try:
    while True:
    clear_console()
    print text_to_ascii_text(font, text[index:], cols, lines)
    index += 1
    if index > len(text):
    index = 0
    time.sleep(0.1)
    except KeyboardInterrupt:
    pass
    """Main"""
    text = 'Scrolling ASCII text in console.'

    font = ImageFont.load_default()
    # font = ImageFont.truetype('arial.ttf', 16)

    # get size of space char
    space_width = get_text_size(font, ' ')[0]

    # get size of text
    text_height = get_text_size(font, text)[1]

    # resize console
    cols = 100
    lines = int(text_height * 1.25)
    init_console(cols, lines)

    # add some padding to the text
    padding = ' ' * int(cols / space_width + 1)
    text = padding + text

    index = 0
    try:
    while True:
    clear_console()
    print(text_to_ascii_text(font, text[index:], cols, lines))
    index += 1
    if index > len(text):
    index = 0
    time.sleep(0.1)
    except KeyboardInterrupt:
    pass


    if __name__ == '__main__':
    main()
    main()
  3. delimitry created this gist Jun 12, 2014.
    66 changes: 66 additions & 0 deletions scrolling_text.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    import os
    import time
    from PIL import Image, ImageDraw, ImageFont

    def init_console(cols, lines):
    os.system('mode con: cols=%s lines=%s' % (cols, lines))

    def clear_console():
    os.system('cls' if os.name == 'nt' else 'clear')

    def get_text_size(font, text):
    return font.getsize(text)

    def text_to_ascii_text(font, text, cols, lines):
    image = Image.new('RGB',(cols - 1, lines - 1), (255, 255, 255))
    draw = ImageDraw.Draw(image)
    draw.text((0, 0), text, fill='black', font=font)
    width, height = image.size
    pixels = image.load()
    # convert image text pixels to ascii
    out = ''
    for y in xrange(height):
    for x in xrange(width):
    pix = pixels[x, y]
    if pix != (255, 255, 255):
    out += '#'
    else:
    out += ' '
    out += '\n'
    return out

    def main():
    text = 'Scrolling ascii text in console.'

    font = ImageFont.load_default()
    #font = ImageFont.truetype('arial.ttf', 16)

    # get size of space char
    space_width = get_text_size(font, ' ')[0]

    # get size of text
    text_height = get_text_size(font, text)[1]

    # resize console
    cols = 100
    lines = int(text_height * 1.25)
    init_console(cols, lines)

    # add some padding to the text
    padding = ' ' * (cols / space_width + 1)
    text = padding + text

    index = 0
    try:
    while True:
    clear_console()
    print text_to_ascii_text(font, text[index:], cols, lines)
    index += 1
    if index > len(text):
    index = 0
    time.sleep(0.1)
    except KeyboardInterrupt:
    pass

    if __name__ == '__main__':
    main()