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

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

Thank you!

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值