"""最近接触抖音,于时制作了这个程序,它能把图片转换成字符画,支持gif图片,即输入gif图,输出动态字符画
作者:李兴球,风火轮少儿编程出品,主要原理其实很简单就是拆帧,图片转灰度,映射到字符集,再转文本字符,合帧。"""
from tkinter.filedialog import *
import os
from image2char import * #需要此模块请联系本人
srcfolder = os.getcwd() + os.sep + "frames" + os.sep #帧存放文件夹
txtoutput = os.getcwd() + os.sep + "txtoutput" + os.sep #转为文本存放目录
dstoutput = os.getcwd() + os.sep + "dstoutput" + os.sep #文本转图存放目录
if not os.path.exists(srcfolder): os.mkdir(srcfolder)
if not os.path.exists(txtoutput): os.mkdir(txtoutput)
if not os.path.exists(dstoutput): os.mkdir(dstoutput)
"出现文件选择对话框"
imagefile = askopenfilename(filetypes=[("gif files", "*.gif"),("all files", "*.*")])
print(imagefile)
im = Image.open(imagefile)
size = im.size
print("图像大小:",size)
basename = os.path.basename(imagefile)
basename = os.path.splitext(basename)[0]
srcframes = splitframe(imagefile,srcfolder) #返回图像序列帧到srcfolder文件夹
framescount = len(srcframes)
print("帧数:",framescount)
txtfiles = [ txtoutput + (4 - len(str(i))) * "0" + str(i) + ".txt" for i in range(framescount)]
dstframes = [ dstoutput + (4 - len(str(i))) * "0" + str(i) + "_dst.png" for i in range(framescount)]
for i in range(framescount):
image2char(srcframes[i],txtfiles[i],0.2)
print("第",str(i+1),"帧转换完毕。")
char2image(txtfiles[i],dstframes[i],(2*size[0],2*size[1]),"#0000ff")
#print("字符转帧完毕。")
print()
print("开始生成字符gif图...")
mergeframe(dstframes,basename + "_char.gif")
print("生成的文件名为:",basename + "_char.gif")
input("按回车键结束。本程序作者:李兴球,风火轮少儿编程 www.scratch8.net")