引言

学python的时候有点无聊,找了有意思的,并简单的python小程序玩一玩,就找的了ybc_art,网上关于ybc_art的博客并不多,就随便写了写。
准备工作:
有python3的朋友可以使用pip install ybc_art下载
没有的去官网去下载相关库

代码

import ybc_art as art
text = "01"
words = art.text2art(text, "larry3d")
print(words)

#效果展示
__ _
/'__`\ /' \
/\ \/\ \ /\_, \
\ \ \ \ \ \/_/\ \
\ \ \_\ \ \ \ \
\ \____/ \ \_\
\/___/ \/_/

源码分析

对于艺术字,可能只用一种,既然它有这么多参数,那么一定有对应的样式集合,点卡art.text2art所在的引用源码,得到了下面的函数

def text2art(text, font=DEFAULT_FONT, chr_ignore=True):
"""
This function print art text
:param text: input text
:type text:str
:param font: input font
:type font:str
:param chr_ignore: ignore not supported character
:type chr_ignore:bool
:return: artText as str
"""
try:
split_list = []
result_list = []
letters = standard_dic
text_temp = text
if font.lower() in font_map.keys():
letters = font_map[font.lower()][0]
if font_map[font.lower()][1] == True:
text_temp = text.lower()
for i in text_temp:
if (ord(i) == 9) or (ord(i) == 32 and font == "block"):
continue
if (i not in letters.keys()) and (chr_ignore == True):
continue
if len(letters[i]) == 0:
continue
split_list.append(letters[i].split("\n"))
if len(split_list) == 0:
return ""
for i in range(len(split_list[0])):
temp = ""
for j in range(len(split_list)):
if j > 0 and (i == 1 or i == len(split_list[0])-2) and font == "block":
temp = temp+" "
temp = temp + split_list[j][i]
result_list.append(temp)
return(("\n").join(result_list))

except KeyError:
print("[Error] Invalid Char!")
except Exception:
print("[Error] Print Faild!")

里面解释的比较详细,而我们要注重的是font_map这个字典,里边包含了所有的样式,点开便得到了很多样式

font_map={"block":[block_dic,True],"banner":[banner_dic,False],"standard":[standard_dic,False],"avatar":[avatar_dic,True],
"basic":[basic_dic,True],"bulbhead":[bulbhead_dic,True],"chunky":[chunky_dic,False],"coinstak":[coinstak_dic,False],
"contessa":[contessa_dic,False],"contrast":[contrast_dic,True],"cyberlarge":[cyberlarge_dic,True],"cybermedium":[cybermedium_dic,True],
"doom":[doom_dic,False],"dotmatrix":[dotmatrix_dic,False],"drpepper":[drpepper_dic,False],
"epic":[epic_dic,True],"fuzzy":[fuzzy_dic,False],"isometric1":[isometric1_dic,True],"isometric2":[isometric2_dic,True],
"isometric3":[isometric3_dic,True],"isometric4":[isometric4_dic,True],"larry3d":[larry3d_dic,False],
"nancyj":[nancyj_dic,False],"ogre":[ogre_dic,False],"rectangles":[rectangles_dic,False],"roman":[roman_dic,False],
"rounded":[rounded_dic,False],"rowancap":[rowancap_dic,True],"script":[script_dic,False],
"serifcap":[serifcap_dic,True],"shadow":[shadow_dic,False],"slant":[slant_dic,False],"speed":[speed_dic,False],
"starwars":[starwars_dic,False],"stop":[stop_dic,False],"thin":[thin_dic,False],"usaflag":[usaflag_dic,False],"standard":[standard_dic,False],
"3-d":[dic_3d,False],"3x5":[dic_3x5,False],"5lineoblique":[dic_5lineoblique,False],"alphabet":[alphabet_dic,False],"banner3-D":[banner3d_dic,True],
"banner3":[banner3_dic,True],"banner4":[banner4_dic,True],"bell":[bell_dic,False],"catwalk":[catwalk_dic,False],"colossal":[colossal_dic,False]}

然后我们全部演示一下:

import ybc_art as art
#引用我们刚才看到的样式字典
from ybc_art.font_map import *

text = "01"
for ty in font_map.keys():
words = art.text2art(text,ty)
print(words)
#效果展示

.----------------. .----------------.
| .--------------. || .--------------. |
| | ____ | || | __ | |
| | .' '. | || | / | | |
| | | .--. | | || | `| | | |
| | | | | | | || | | | | |
| | | `--' | | || | _| |_ | |
| | '.____.' | || | |_____| | |
| | | || | | |
| '--------------' || '--------------' |
'----------------' '----------------'

### #
# # ##
# # # #
# # #
# # #
# # #
### #####

......
......
......
.d8888b. d888
d88P Y88b d8888
888 888 888
888 888 888
888 888 888
888 888 888
Y88b d88P 888
"Y8888P" 8888888