如何使用Python 3.x在控制台应用程序中创建ASCII动画?

9
5个回答

12

我刚刚将我的用ASCII动画替换成动态gif的示例从此处移植到了python。你需要从这里安装pyglet库,因为不幸的是python没有内置的动态gif支持。希望你喜欢它 :)

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')

我没有在Python 3.x上测试过它,只有2.6在我的电脑上。如果有人能在3.x上测试一下:那就太好了。 - Philip Daubmeier
我已经在Python 3.5.2上尝试过了,但不幸的是编译器报告出现了这样的错误:TypeError: ord()期望长度为1的字符串,但找到了int。 SO中的一些答案表明应该删除ord()函数。但是当你这样做时,它也会从相同的行中断开,如:TypeError:元组索引必须是整数或切片,而不是浮点数。所以我相信我需要有人来测试这个问题 :) - Prometheus
那是一篇旧帖子。但是,第21行应该是:"outstr += chars [int((pixel * (len(chars) - 1)) / 255)] +"。但是枚举数据会从底部向上取像素,所以在控制台中动画是倒置的。 - Timofey Kargin

10

这正是我创建asciimatics的应用场景。

它是一个跨平台的控制台API,支持从丰富的文本效果中生成动画场景。它已被证明可以在各种CentOS、Windows和OSX版本上运行。

可以从图库中获取一些示例。这里有一个示例,类似于其他答案中提供的动画GIF代码。

Colour images

我想你只是想找到一种动画的方式,但如果你真的想复制蒸汽火车,你可以将其转换为一个Sprite,并给它一个只在屏幕上运行的路径,然后将其作为场景的一部分播放。对象的完整说明可以在文档中找到。


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:我在Windows下测试了这个,但它不起作用:(。未来有三种基本选择:(请选择)

  1. 安装第三方的Windows-curses库(http://adamv.com/dev/python/curses/
  2. 将Windows-curses补丁应用到Python上(http://bugs.python.org/msg94309
  3. 放弃curses,使用其他东西。

你安装了pyglet吗?出现了哪个错误信息?我像我说的那样在Windows上用Python 2.6和cmd控制台测试过,它可以工作。 - Philip Daubmeier
顺便说一句:你不一定需要使用curses,就像你在我的答案中看到的那样。 - Philip Daubmeier

2

3
回答一个有100赏金的问题时,仅发布单个链接作为答案是不被允许的。 - Adam

2
我成功将Philip Daubmeier的解决方案移植到了Python 3(并添加了颜色映射)。主要问题在于ord函数需要被排除,因为Python 3 - 字节串索引直接返回ASCII值,而不是该位置上的字符(请参阅这里这里)。 我创建了一个Git repo,欢迎贡献代码(更好的性能将是理想的,想要邀请请私信我)。 repo: https://github.com/sebibek/gif2ascii

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接