9

I would like to port this question to Python (Windows + Linux + Mac Os)

How to create ASCII animation in Windows Console application using C#?

Thank you!

Community
  • 1
  • 1
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147

5 Answers5

12

I just ported my example with the animated gif to ASCII animation from my answer here to python. You will need to install the pyglet library from here, as python unfortunately has no built-in animated-gif support. Hope you like it :)

import pyglet, sys, os, time

def animgif_to_ASCII_animation(animated_gif_path):
    # map greyscale to characters
    chars = ('#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ')
    clear_console = 'clear' if os.name == 'posix' else 'CLS'

    # load image
    anim = pyglet.image.load_animation(animated_gif_path)

    # Step through forever, frame by frame
    while True:
        for frame in anim.frames:

            # Gets a list of luminance ('L') values of the current frame
            data = frame.image.get_data('L', frame.image.width)

            # Built up the string, by translating luminance values to characters
            outstr = ''
            for (i, pixel) in enumerate(data):
                outstr += chars[(ord(pixel) * (len(chars) - 1)) / 255] + \
                          ('\n' if (i + 1) % frame.image.width == 0 else '')

            # Clear the console
            os.system(clear_console)

            # Write the current frame on stdout and sleep
            sys.stdout.write(outstr)
            sys.stdout.flush()
            time.sleep(0.1)

# run the animation based on some animated gif
animgif_to_ASCII_animation(u'C:\\some_animated_gif.gif')
Community
  • 1
  • 1
Philip Daubmeier
  • 14,584
  • 5
  • 41
  • 77
  • Didnt test it with python 3.x, only got 2.6 on my PC. If anyone could test that on 3.x: would be great. – Philip Daubmeier May 07 '10 at 01:15
  • I've tried this on python 3.5.2, but unfortunately compiler states that there is an error like this: TypeError: ord() expected string of length 1, but int found . Some of the answers in SO states that ord() function should be deleted. but when you did that, it also breaks from the same line as: TypeError: tuple indices must be integers or slices, not float. So I believe I need someone to test this too :) – Prometheus Jan 20 '17 at 13:27
  • That's an old post. But still, 21st line should be: "outstr += chars[int((pixel * (len(chars) - 1)) / 255)] + \". But enumerate(data) takes pixels from the bottom to the top, so animation in a console is upside down. – Timofey Kargin Dec 26 '21 at 09:55
10

This is precisely the sort of application that I created asciimatics for.

It is a cross-platform console API with support for generating animated scenes from a rich set of text effects. It has been proved to work on various flavours of CentOS and Windows and OSX.

Samples of what is possible are available from the gallery. Here's a sample similar to the animated GIF code provided in other answers.

Colour images

I assume you're just looking for a way to do any animation, but if you really wanted to replicate the steam train, you could convert it to a Sprite and give it a Path that just runs it across the Screen, then play it as part of a Scene. Full explanations of the objects can be found in the docs.

Peter Brittain
  • 13,489
  • 3
  • 41
  • 57
3

Simple console animation, tested on python3 in Ubuntu. addch() doesn't like that non-ascii character, but it works in addstr().

#this comment is needed in windows:
#  encoding=latin-1
def curses(win):
    from curses import use_default_colors, napms, curs_set
    use_default_colors()
    win.border()
    curs_set(0)

    row, col = win.getmaxyx()
    anim = '.-+^°*'
    y = int(row / 2)
    x = int((col - len(anim))/2)
    while True:
        for i in range(6):
            win.addstr(y, x+i, anim[i:i+1])
            win.refresh()
            napms(100)
            win.addch(y, x+i, ' ')

if __name__ == "__main__":
    from curses import wrapper
    wrapper(curses)

@Philip Daubmeier: I've tested this under Windoze and it doesn't work :(. There's three basic options going forward: (please choose)

  1. Install a third-party windows-curses library (http://adamv.com/dev/python/curses/)
  2. Apply a windows-curses patch to python (http://bugs.python.org/msg94309)
  3. Abandon curses altogether for something else.
bukzor
  • 37,539
  • 11
  • 77
  • 111
2

Colorama: http://pypi.python.org/pypi/colorama

ja.
  • 4,245
  • 20
  • 22
2

Well, I managed to port Philip Daubmeier's solution to python 3 (and also added color mapping to it). The main problem was the ord-function, which needed to be left out, as Python 3 - bytestring indexing returns the ASCII-value directly, instead of the char at that position (see here and here..). I created a Git repo, feel free to contribute (better performance would be desirable-> pm for invitation):

repo: https://github.com/sebibek/gif2ascii

seebi
  • 488
  • 5
  • 9