9

この質問を Python (Windows + Linux + Mac Os) に移植したいと思います。

C# を使用して Windows コンソール アプリケーションで ASCII アニメーションを作成するには?

ありがとうございました!

4

5 に答える 5

12

ここでの回答からpythonへのアニメーションgifからASCIIアニメーションへの例を移植しました。残念ながら、Pythonにはanimated-gifのサポートが組み込まれていないため、ここからpygletライブラリをインストールする必要があります。あなたがそれを好きになることを願っています:)

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')
于 2010-05-07T01:13:58.817 に答える
3

Ubuntu の python3 でテストされたシンプルなコンソール アニメーション。addch() は非 ASCII 文字が好きではありませんが、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: Windoze でこれをテストしましたが、動作しません :(。3 つの基本的なオプションがあります: (選択してください)

  1. サードパーティの windows-curses ライブラリをインストールします ( http://adamv.com/dev/python/curses/ )
  2. windows-curses パッチを Python に適用します ( http://bugs.python.org/msg94309 )
  3. 他の何かのために呪いを完全に放棄します。
于 2010-05-07T06:39:15.010 に答える
2

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

于 2010-05-05T04:49:09.250 に答える