La LIP offre un moyen très simple de le faire. Vous pouvez rendre le texte sur une image en noir et blanc et convertir ce bitmap en un flux de chaînes de caractères en remplaçant les pixels noirs et blancs par des caractères.
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)
Il rend le résultat suivant :
####### ## ####### ## ##
## ### ## ## ## ### ## ##
## ## ## ## ## ## ## ##
## ## ## ## #### ###### #### ###### ## ## ## ##
## ## ## ### ## ### ## ## ## ### ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
###### ## ## ## ## ## ## ## ## ## ###### ## ##
## ## # ## ## ## ## ## ## ## ## ## ##
## #### ## ## ## ## ## ## ## ## ## ##
## #### ## ## ## ## ## ## ## ## ## ##
## ## ### ## ## #### ## ## ## ## ########
##
##
###
##
###
J'ai fait un exemple un peu plus complet avec un style fonctionnel.
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])])
2 votes
Vous pourriez faire appel à figlet, un utilitaire Unix.
1 votes
@Marcin : merci, figlet semble sympa ! Certaines personnes semblent même avoir implémenté des wrappers python pour lui.
0 votes
Heureux d'aider. N'hésitez pas à répondre à votre propre question en fournissant des informations supplémentaires à ce sujet.
1 votes
Ici vous avez le portage de figlet en pur python : github.com/pwaller/pyfiglet
1 votes
Je ne connais pas figlet, (qui sonne mieux), mais ce que vous décrivez dans votre question est exactement comme la sortie de
banner
. Bonne chance.