Ham*_*jan 5 animation console-application ascii-art python-3.x
我想将这个问题移植到Python(Windows + Linux + Mac Os)
如何使用C#在Windows控制台应用程序中创建ASCII动画?
谢谢!
Phi*_*ier 11
我只是移植用gif动画,以ASCII动画从我的答案我的例子在这里给蟒蛇.你需要从这里安装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')
Run Code Online (Sandbox Code Playgroud)