Ich habe ein Programm, das viel ausgibt, und Ich möchte, dass ein Teil dieser Ausgabe wirklich auffällt. Ein Weg könnte sein, wichtigen Text mit ASCII-Kunst zu rendern, wie dieser Webdienst zum Beispiel:
# # ## ##### # # # # # ####
# # # # # # ## # # ## # # #
# # # # # # # # # # # # # #
# ## # ###### ##### # # # # # # # # ###
## ## # # # # # ## # # ## # #
# # # # # # # # # # # ####
andere Lösungen könnten farbig oder fett gedruckt sein. Also, wie macht man so etwas einfach in Python?
pyfiglet - pure Python Implementierung von http://www.figlet.org
pip install pyfiglet
termcolor - Hilfsfunktionen für die ANSI-Farbformatierung
pip install termcolor
colorama - Unterstützung mehrerer Plattformen (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'])
$ python print-warning.py
$ python print-warning.py | cat .___ ___. __ _______. _______. __ __ _______ __ | \/| | |/|/|| | | | ____ || | | \/| | | (---- `| (----` | | | | | __ | .____.] | | \/| | |\\\\ | | | | | __ | | | | | | | .----) | .----) | | | | `----. | ____ | __ | | __ | __ | __ | __ | ______/______/__ | __ | ______ || ______ | (__)
PIL bietet eine coole Möglichkeit, dies ganz einfach zu tun. Sie können den Text in ein s/w-Bild rendern und diese Bitmap in einen String-Stream konvertieren, indem Sie die schwarzen und weißen Pixel in Zeichen umwandeln.
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)
Es gibt das nächste Ergebnis aus:
####### ## ####### ## ##
## ### ## ## ## ### ## ##
## ## ## ## ## ## ## ##
## ## ## ## #### ###### #### ###### ## ## ## ##
## ## ## ### ## ### ## ## ## ### ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
###### ## ## ## ## ## ## ## ## ## ###### ## ##
## ## # ## ## ## ## ## ## ## ## ## ##
## #### ## ## ## ## ## ## ## ## ## ##
## #### ## ## ## ## ## ## ## ## ## ##
## ## ### ## ## #### ## ## ## ## ########
##
##
###
##
###
Ich habe ein etwas umfassenderes Beispiel mit funktionalem Stil gemacht.
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])])
Das macht Spaß. Ich habe herausgefunden, wie man PIL (natürlich die "Pillow" -Gabel) und Numpy verwendet, um dies vollständig "vektorisiert" zu tun, d. H. Ohne Schleifen:
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))
## ##
## ## ## ## ##
## ## ## ##
## ## ## ##### ##### #### ## ## ####
## ## ## ## ## ## ## ## ##### ## ##
######## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ###### ## ######
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## # ## ## #
## ## ## ### ## ## #### ## ####