Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何轻松打印ascii艺术文本?_Python_User Interface_Text_Command Line_Logging - Fatal编程技术网

Python 如何轻松打印ascii艺术文本?

Python 如何轻松打印ascii艺术文本?,python,user-interface,text,command-line,logging,Python,User Interface,Text,Command Line,Logging,我有一个转储大量输出的程序,我希望其中一些输出能够真正脱颖而出。一种方法可以是使用ascii艺术呈现重要文本,例如: # # ## ##### # # # # # #### # # # # # # ## # # ## # # # # # # # # # # # # # # # # # # ## # ###### ##### # # # # # # # # ### ## ##

我有一个转储大量输出的程序,我希望其中一些输出能够真正脱颖而出。一种方法可以是使用ascii艺术呈现重要文本,例如:

 #    #   ##   #####  #    # # #    #  ####  
 #    #  #  #  #    # ##   # # ##   # #    # 
 #    # #    # #    # # #  # # # #  # #      
 # ## # ###### #####  #  # # # #  # # #  ### 
 ##  ## #    # #   #  #   ## # #   ## #    # 
 #    # #    # #    # #    # # #    #  ####  
其他解决方案可以是彩色或粗体输出。那么如何在Python中轻松完成这类工作呢?

  • pyfiglet-的纯Python实现

  • termcolor-ANSI颜色格式的辅助函数

    pip install termcolor
    
  • colorama-多平台支持(Windows)

例子

$python print-warning.py | cat .___ ___. __ _______. _______. __ __ _______ __ | \/ | | | / | / || | | | | ____|| | | \ / | | | | (----` | (----`| | | | | |__ | | | |\/| | | | \ \ \ \ | | | | | __| | | | | | | | | .----) | .----) | | | | `----.| |____ |__| |__| |__| |__| |_______/ |_______/ |__| |_______||_______|(__)
PIL提供了一种非常简单的方法来实现这一点。 您可以将文本渲染到黑白图像上,并将该位图转换为字符串流,将黑白像素替换为字符

from PIL 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)
 
它将呈现下一个结果:

 #######                 ##                              #######   ##  ##
 ##   ###           ##   ##                              ##   ###  ##  ##
 ##    ##           ##   ##                              ##    ##  ##  ##
 ##    ## ##    ## ####  ######     ####    ######       ##    ##  ##  ##
 ##    ##  ##  ###  ##   ###  ##   ##  ##   ###  ##      ##    ##  ##  ##
 ##   ##   ##  ##   ##   ##   ##  ##    ##  ##   ##      ##   ##   ##  ##
 ######    ##  ##   ##   ##   ##  ##    ##  ##   ##      ######    ##  ##
 ##         ## #    ##   ##   ##  ##    ##  ##   ##      ##        ##  ##
 ##         ####    ##   ##   ##  ##    ##  ##   ##      ##        ##  ##
 ##         ####    ##   ##   ##   ##  ##   ##   ##      ##        ##  ##
 ##          ##     ###  ##   ##    ####    ##   ##      ##        ##  ########
             ##
             ##
           ###             
         ##
       ###
我制作了一个更全面的函数式示例

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])])

这很有趣。我已经想出了如何使用PIL(当然是“枕头”叉)和Numpy来实现完全“矢量化”,即没有循环:

text=“你好”
从PIL导入图像、ImageDraw、ImageFont
将numpy作为np导入
myfont=ImageFont.truetype(“verdanab.ttf”,12)
size=myfont.getsize(文本)
img=图像。新(“1”,尺寸,“黑色”)
draw=ImageDraw.draw(img)
draw.text((0,0),文本,“白色”,font=myfont)
像素=np.array(img,dtype=np.uint8)
chars=np.array(['',#'],dtype=“U1”)[像素]
strings=chars.view('U'+str(chars.shape[1])).flatte()
打印(“\n”.join(字符串))
####
##    ##  ##      ##   ##                            
##    ##          ##   ##                            
##    ##  ##     ##### #####    ####   ## ##  ####   
##    ##  ##      ##   ##  ##  ##  ##  ##### ##  ##  
########  ##      ##   ##  ##  ##  ##  ##    ##  ##  
##    ##  ##      ##   ##  ##  ######  ##    ######  
##    ##  ##      ##   ##  ##  ##      ##    ##      
##    ##  ##      ##   ##  ##  ##   #  ##    ##   #  
##    ##  ##       ### ##  ##   ####   ##     ####   

您可以调用figlet,一个unix实用程序。@Marcin:谢谢,figlet看起来不错!有些人甚至似乎已经为它实现了python包装器。很高兴能提供帮助。请随意回答您自己的问题,并提供进一步的信息。这里有纯python中的figlet端口:不知道figlet(听起来更好),但您在问题中描述的内容与
横幅的输出完全相同。祝你好运。大a*s导弹警告+1!:)@布兰登贝特森:如果你被导弹击中,那么你的眼睛可能不是唯一燃烧的东西。
$ python print-warning.py 
$ python print-warning.py | cat .___ ___. __ _______. _______. __ __ _______ __ | \/ | | | / | / || | | | | ____|| | | \ / | | | | (----` | (----`| | | | | |__ | | | |\/| | | | \ \ \ \ | | | | | __| | | | | | | | | .----) | .----) | | | | `----.| |____ |__| |__| |__| |__| |_______/ |_______/ |__| |_______||_______|(__)
from PIL 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)
 
 #######                 ##                              #######   ##  ##
 ##   ###           ##   ##                              ##   ###  ##  ##
 ##    ##           ##   ##                              ##    ##  ##  ##
 ##    ## ##    ## ####  ######     ####    ######       ##    ##  ##  ##
 ##    ##  ##  ###  ##   ###  ##   ##  ##   ###  ##      ##    ##  ##  ##
 ##   ##   ##  ##   ##   ##   ##  ##    ##  ##   ##      ##   ##   ##  ##
 ######    ##  ##   ##   ##   ##  ##    ##  ##   ##      ######    ##  ##
 ##         ## #    ##   ##   ##  ##    ##  ##   ##      ##        ##  ##
 ##         ####    ##   ##   ##  ##    ##  ##   ##      ##        ##  ##
 ##         ####    ##   ##   ##   ##  ##   ##   ##      ##        ##  ##
 ##          ##     ###  ##   ##    ####    ##   ##      ##        ##  ########
             ##
             ##
           ###             
         ##
       ###
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])])