猫史档案馆


【Python作品分享】彩色字符动画【作品秀】

用户:AbnormalLOGAbnormalLOG查看:0 回复:0 评论:0 创建时间:2020-05-29T20:51:24


【作品展示】

center_image

 

【作品介绍】

升级版字符画转换器(只转换动图)这里还附上了提示,大家再也不会说我在喵了😶😶😶❤❤❤

 

【作品源代码】

from PIL import Image,ImageDraw,ImageSequence
from easygui import*
from time import*
sleep(0.5)
#欢迎使用字符动画生成器
#在使用之前请先安装pillow库和easygui库,并保存在本地
print('初始化完成...')
def img2char(imgobj):
    width, heigth = (60, 60)
    img_original = imgobj.resize((width, heigth))
    img = imgobj.convert('L').resize((width, heigth))
    ASCII_HIGH = '''$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. '''
    img_new = Image.new('RGB', (width*12, heigth*15), 'white')
    draw = ImageDraw.Draw(img_new)
    txt=''
    for y in range(heigth):
        for x in range(width):
            pos = (x, y)
            gray = img.getpixel(pos)
            index = int(gray/256*60)
            txt = ASCII_HIGH[index]+' '
            color = img_original.getpixel(pos)
            draw.text((x*12, y*15), txt, fill=color)
    

    return img_new.resize((width*12, width*12))
def gif2char(imgfile):
    print('打开动图中...')
    sleep(0.5)
    img=Image.open(imgfile)
    frames=ImageSequence.Iterator(img)
    img_colorchars=[]
    print('每一帧图片转换为字符画...')
    sleep(0.5)
    print('字符画绘制中...')
    sleep(0.5)
    for frame in frames:
        colorchar=img2char(frame)
        img_colorchars.append(colorchar)
    print('保存字符画...')
    sleep(0.5)
    img_colorchars[0].save('bcm_colorchar.gif',
    append_images=img_colorchars[1:],
    save_all=True,
    loop=0,
    duration=0.1)
    msgbox('转换完成', '字符动画生成器', 'Ok')
gif2char(input('请输入动图路径'))

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页