introduction
When learning python was a bit boring, I found something interesting and played with simple python applets, and I found ybc_art. There are not many blogs about ybc_art on the Internet, so I just wrote it casually.
Preparation:
friends with python3 can use pip install ybc_art to download. If
not, go to the official website to download related libraries
Code
import ybc_art as art
text = "01"
words = art.text2art(text, "larry3d")
print(words)
#效果展示
__ _
/'__`\ /' \
/\ \/\ \ /\_, \
\ \ \ \ \ \/_/\ \
\ \ \_\ \ \ \ \
\ \____/ \ \_\
\/___/ \/_/
Source code analysis
For art characters, only one kind may be used. Since it has so many parameters, there must be a corresponding style set. Click the source code where art.text2art is located, and get the following function
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!")
The explanation inside is more detailed, and what we should pay attention to is the font_map dictionary, which contains all the styles, and you can get many styles by clicking on it.
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]}
Then we all demonstrate:
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