如何轻松打印ascii-art文字?

时间:2022-12-16 00:23:53

I have a program that dumps a lot of output, and I want some of that output to really stand out. One way could be to render important text with ascii art, like this web service does for example:

我有一个转储大量输出的程序,我希望其中一些输出能够真正脱颖而出。一种方法是使用ascii art渲染重要文本,例如此Web服务可以:

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

other solutions could be colored or bold output. So how to do this sort of stuff easily in Python?

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

2 个解决方案

#1


68  

  • pyfiglet - pure Python implementation of http://www.figlet.org

    pyfiglet - http://www.figlet.org的纯Python实现

    pip install pyfiglet
    
  • termcolor - helper functions for ANSI color formatting

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

    pip install termcolor
    
  • colorama - multiplatform support (Windows)

    colorama - 多平台支持(Windows)

    pip install colorama
    
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'])

Example

$ python print-warning.py 

如何轻松打印ascii-art文字?

$ python print-warning.py | cat
.___  ___.  __       _______.     _______. __   __       _______  __
|   \/   | |  |     /       |    /       ||  | |  |     |   ____||  |
|  \  /  | |  |    |   (----`   |   (----`|  | |  |     |  |__   |  |
|  |\/|  | |  |     \   \        \   \    |  | |  |     |   __|  |  |
|  |  |  | |  | .----)   |   .----)   |   |  | |  `----.|  |____ |__|
|__|  |__| |__| |_______/    |_______/    |__| |_______||_______|(__)

#2


18  

PIL gives a cool way to do this very simple. You can render the text onto a b/w image and convert that bitmap to a string stream replacing the black and white pixels to chars.

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)

It renders the next result:

它呈现下一个结果:

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

I made a little more comprehensive example with functional style.

我用功能风格做了一个更全面的例子。

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

#1


68  

  • pyfiglet - pure Python implementation of http://www.figlet.org

    pyfiglet - http://www.figlet.org的纯Python实现

    pip install pyfiglet
    
  • termcolor - helper functions for ANSI color formatting

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

    pip install termcolor
    
  • colorama - multiplatform support (Windows)

    colorama - 多平台支持(Windows)

    pip install colorama
    
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'])

Example

$ python print-warning.py 

如何轻松打印ascii-art文字?

$ python print-warning.py | cat
.___  ___.  __       _______.     _______. __   __       _______  __
|   \/   | |  |     /       |    /       ||  | |  |     |   ____||  |
|  \  /  | |  |    |   (----`   |   (----`|  | |  |     |  |__   |  |
|  |\/|  | |  |     \   \        \   \    |  | |  |     |   __|  |  |
|  |  |  | |  | .----)   |   .----)   |   |  | |  `----.|  |____ |__|
|__|  |__| |__| |_______/    |_______/    |__| |_______||_______|(__)

#2


18  

PIL gives a cool way to do this very simple. You can render the text onto a b/w image and convert that bitmap to a string stream replacing the black and white pixels to chars.

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)

It renders the next result:

它呈现下一个结果:

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

I made a little more comprehensive example with functional style.

我用功能风格做了一个更全面的例子。

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