sta*_*tti 47 python user-interface logging command-line text
我有一个转储大量输出的程序,我希望其中一些输出能够真正脱颖而出.一种方法是使用ascii art渲染重要文本,例如这个Web服务就是这样:
# # ## ##### # # # # # ####
# # # # # # ## # # ## # # #
# # # # # # # # # # # # # #
# ## # ###### ##### # # # # # # # # ###
## ## # # # # # ## # # ## # #
# # # # # # # # # # # ####
Run Code Online (Sandbox Code Playgroud)
其他解决方案可以是彩色或粗体输出.那么如何在Python中轻松完成这类工作呢?
jfs*_*jfs 82
pyfiglet - http://www.figlet.org的纯Python实现
pip install pyfiglet
Run Code Online (Sandbox Code Playgroud)termcolor - ANSI颜色格式的辅助函数
pip install termcolor
Run Code Online (Sandbox Code Playgroud)colorama - 多平台支持(Windows)
pip install colorama
Run Code Online (Sandbox Code Playgroud)import sys
from colorama import init
init(strip=not sys.stdout.isatty()) # strip colors if stdout is redirected
from termcolor import cprint
from pyfiglet import figlet_format
cprint(figlet_format('missile!', font='starwars'),
'yellow', 'on_red', attrs=['bold'])
Run Code Online (Sandbox Code Playgroud)
$ python print-warning.py
Run Code Online (Sandbox Code Playgroud)

$ python print-warning.py | cat .___ ___. __ _______. _______. __ __ _______ __ | \/ | | | / | / || | | | | ____|| | | \ / | | | | (----` | (----`| | | | | |__ | | | |\/| | | | \ \ \ \ | | | | | __| | | | | | | | | .----) | .----) | | | | `----.| |____ |__| |__| |__| |__| |_______/ |_______/ |__| |_______||_______|(__)
jsh*_*erd 25
PIL提供了一种非常简单的方法.您可以将文本渲染到ab/w图像上,并将该位图转换为字符串流,将黑白像素替换为字符.
import Image, ImageFont, ImageDraw
ShowText = 'Python PIL'
font = ImageFont.truetype('arialbd.ttf', 15) #load the font
size = font.getsize(ShowText) #calc the size of text in pixels
image = Image.new('1', size, 1) #create a b/w image
draw = ImageDraw.Draw(image)
draw.text((0, 0), ShowText, font=font) #render the text to the bitmap
for rownum in range(size[1]):
#scan the bitmap:
# print ' ' for black pixel and
# print '#' for white one
line = []
for colnum in range(size[0]):
if image.getpixel((colnum, rownum)): line.append(' '),
else: line.append('#'),
print ''.join(line)
Run Code Online (Sandbox Code Playgroud)
它呈现下一个结果:
####### ## ####### ## ##
## ### ## ## ## ### ## ##
## ## ## ## ## ## ## ##
## ## ## ## #### ###### #### ###### ## ## ## ##
## ## ## ### ## ### ## ## ## ### ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
###### ## ## ## ## ## ## ## ## ## ###### ## ##
## ## # ## ## ## ## ## ## ## ## ## ##
## #### ## ## ## ## ## ## ## ## ## ##
## #### ## ## ## ## ## ## ## ## ## ##
## ## ### ## ## #### ## ## ## ## ########
##
##
###
##
###
Run Code Online (Sandbox Code Playgroud)
我用功能风格做了一个更全面的例子.
import Image, ImageFont, ImageDraw
ShowText = 'Python PIL'
font = ImageFont.truetype('arialbd.ttf', 15) #load the font
size = font.getsize(ShowText) #calc the size of text in pixels
image = Image.new('1', size, 1) #create a b/w image
draw = ImageDraw.Draw(image)
draw.text((0, 0), ShowText, font=font) #render the text to the bitmap
def mapBitToChar(im, col, row):
if im.getpixel((col, row)): return ' '
else: return '#'
for r in range(size[1]):
print ''.join([mapBitToChar(image, c, r) for c in range(size[0])])
Run Code Online (Sandbox Code Playgroud)
这个很有趣。我已经弄清楚了如何使用PIL(当然是“枕头”叉子)和Numpy来完全实现“矢量化”,即没有循环:
text = "Hi there"
from PIL import Image, ImageDraw, ImageFont
import numpy as np
myfont = ImageFont.truetype("verdanab.ttf", 12)
size = myfont.getsize(text)
img = Image.new("1",size,"black")
draw = ImageDraw.Draw(img)
draw.text((0, 0), text, "white", font=myfont)
pixels = np.array(img, dtype=np.uint8)
chars = np.array([' ','#'], dtype="U1")[pixels]
strings = chars.view('U' + str(chars.shape[1])).flatten()
print( "\n".join(strings))
Run Code Online (Sandbox Code Playgroud)
## ##
## ## ## ## ##
## ## ## ##
## ## ## ##### ##### #### ## ## ####
## ## ## ## ## ## ## ## ##### ## ##
######## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ###### ## ######
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## # ## ## #
## ## ## ### ## ## #### ## ####
Run Code Online (Sandbox Code Playgroud)