存在百度云里的outstring文件夹里有以上三个文件,将它们下载至本地
打开箭头所指的程序,出现如下界面·:
输入文件名暨图片名称(若图片和程序在同一目录下)
输出文件名暨字符画文件名
可调节字符画的宽和高
自定义字符画的字符组成(建议输入从“笔画复杂到笔画简单的字符”)
点击制作便生成一幅以输出文件名为名的字符画
效果如下:
点击格式》字体》大小,调节成更好效果
链接:https://pan.baidu.com/s/1CWCHGjaW8Cm9fQyBiKIpTw 密码:kqto
#!/usr/bin/python3 # -*- coding: UTF-8 -*- from PIL import Image import tkinter import tkinter.messagebox #import os '''import argparse #命令行输入参数处理 parser = argparse.ArgumentParser() parser.add_argument('file') #输入文件 parser.add_argument('-o', '--output') #输出文件 parser.add_argument('--width', type = int, default = 80) #输出字符画宽 parser.add_argument('--height', type = int, default = 80) #输出字符画高 #获取参数 args = parser.parse_args()''' IMG = r'7.jpg' WIDTH = 100 HEIGHT = 100 OUTPUT = "output.txt" choice = True ascii_char = list(r"$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,"^`'. ") myascii_char = list(r"我爱你") # 将256灰度映射到字符上 def get_char(r,g,b,alpha = 256): if alpha == 0: return ' ' gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b) if choice == False: length = len(myascii_char) unit = (256.0 + 1)/length return myascii_char[int(gray/unit)] length = len(ascii_char) unit = (256.0 + 1)/length return ascii_char[int(gray/unit)] def showmyhelp(): helproot = tkinter.Tk() helproot.title("帮助") showtext = tkinter.Label(helproot,justify='left',text= '1、输入文件名为当前路径图片文件名(若图片在其他路径则前加路径) 2、输出文件名为字符画文件名 3、宽/高为字符画宽度/高度(适当调整可达最好效果) 4、记事本打开字符画时,字号调小效果更佳') showtext.grid(row=0,column=0) def run(): if mychar.get() != '': global choice global myascii_char choice = False myascii_char = list(mychar.get()) #tkinter.messagebox.showinfo(myascii_char,choice) IMG = inname.get() WIDTH = int(width.get()) HEIGHT = int(height.get()) OUTPUT = outname.get() im = Image.open(IMG) im = im.resize((WIDTH,HEIGHT), Image.NEAREST) txt = "" for i in range(HEIGHT): for j in range(WIDTH): txt += get_char(*im.getpixel((j,i))) txt += ' ' #字符画输出到文件 if OUTPUT: with open(OUTPUT,'w') as f: f.write(txt) else: with open("output.txt",'w') as f: f.write(txt) tkinter.messagebox.showinfo("maybe","已完成!!!") #GUI root = tkinter.Tk() root.title("图片转字符画") root.resizable(0,0) inname = tkinter.Entry(root) inname_t = tkinter.Label(root,text="输入文件名") outname = tkinter.Entry(root) outname_t = tkinter.Label(root,text="输出文件名") width = tkinter.Entry(root) width_t = tkinter.Label(root,text="宽") height = tkinter.Entry(root) height_t = tkinter.Label(root,text="高") myhelp = tkinter.Button(root,text="帮助",command=showmyhelp) ok = tkinter.Button(root,text="制作",command=run) inname.grid(row=1,column=1) inname_t.grid(row=1,column=0) outname.grid(row=2,column=1) outname_t.grid(row=2,column=0) width.grid(row=3,column=1) width_t.grid(row=3,column=0) height.grid(row=4,column=1) height_t.grid(row=4,column=0) myhelp.grid(row=0,column=2) ok.grid(row=5,column=2) mychar_t = tkinter.Label(root,text="下面文本框输入自定义字符画的字符组成, 可能效果不是很好(测试功能)") mychar_t.grid(row=6,column=0) mychar = tkinter.Entry(root) mychar.grid(row=7,column=0) inname.insert(0,IMG) outname.insert(0,OUTPUT) width.insert(0,WIDTH) height.insert(0,HEIGHT) #不要试图在一个主窗口中混合使用pack和grid #消息循环 root.mainloop() #GUI