Text-to-ASCII art generator in Python

有些话、适合烂在心里 提交于 2019-12-12 08:34:31

问题


I would like to add an easter egg to one our developers' command line tools, which will greet its user if a certain date is matched. Basically, I'm looking for:

>>> print big_text("Happy\nBirthday")                                                                                   

   .                   _________   _...._    _________   _...._                    
 .'|                   \        |.'      '-. \        |.'      '-. .-.          .- 
<  |                    \        .'```'.    '.\        .'```'.    '.\ \        / / 
 | |             __      \      |       \     \\      |       \     \\ \      / /  
 | | .'''-.   .:--.'.     |     |        |    | |     |        |    | \ \    / /   
 | |/.'''. \ / |   \ |    |      \      /    .  |      \      /    .   \ \  / /    
 |  /    | | `" __ | |    |     |\`'-.-'   .'   |     |\`'-.-'   .'     \ `  /     
 | |     | |  .'.''| |    |     | '-....-'`     |     | '-....-'`        \  /      
 | |     | | / /   | |_  .'     '.             .'     '.                 / /       
 | '.    | '.\ \._,\ '/'-----------'         '-----------'           |`-' /        
 '---'   '---'`--'  `"                    _______                     '..'         
/|        .--.                   .        \  ___ `'.                               
||        |__|                 .'|         ' |--.\  \          .-.          .-     
||        .--..-,.--.      .| <  |         | |    \  '          \ \        / /     
||  __    |  ||  .-. |   .' |_ | |         | |     |  '    __    \ \      / /      
||/'__ '. |  || |  | | .'     || | .'''-.  | |     |  | .:--.'.   \ \    / /       
|:/`  '. '|  || |  | |'--.  .-'| |/.'''. \ | |     ' .'/ |   \ |   \ \  / /        
||     | ||  || |  '-    |  |  |  /    | | | |___.' /' `" __ | |    \ `  /         
||\    / '|__|| |        |  |  | |     | |/_______.'/   .'.''| |     \  /          
|/\'..' /     | |        |  '.'| |     | |\_______|/   / /   | |_    / /           
'  `'-'`      |_|        |   / | '.    | '.            \ \._,\ '/|`-' /            
                         `'-'  '---'   '---'            `--'  `"  '..'             

Is there a package for that?

Credit where credit is due.


回答1:


Author of the TAAG app you linked here. Most of the fonts in TAAG are FIGlet fonts (figlet.org). FIGlet is a command line linux app, but FIGlet drivers have been written in several languages. I released the driver I wrote in JavaScript here:

https://github.com/patorjk/figlet.js

Though that would need to be ported to Python to work. I did a search for FIGlet Python libraries and found this:

https://github.com/pwaller/pyfiglet

I'm not sure how well it works, or how much of the spec it implements, but it looks pretty complete.




回答2:


Here is a code snippet from ActiveState of a Python Banner example. http://code.activestate.com/recipes/577537-banner/




回答3:


I think this question is a bit off topic for Stack Overflow, but you can try to google "ASCII art Python" and get things like: http://www.youtube.com/watch?v=NEWuZfTNoJE

OR you can try to do it yourself, here's an outline:

rows = 13 # Maximum height of character

# 0 is a , 1 is b and so on...
alphabeth = [[
r'''           ''',
r'''           ''',
r'''           ''',
r'''           ''',
r'''           ''',
r'''    __     ''',
r''' .:--.'.   ''',
r'''/ |   \ |  ''',
r'''`" __ | |  ''',
r''' .'.''| |  ''',
r'''/ /   | |_ ''',
r'''\ \._,\ '/ ''',
r''' `--'  `"  ''']]

text = raw_input('Enter text:\n')
c = map(lambda x: ord(x)-ord('a'),text)
for i in range(rows):
    for j in c:
        print alphabeth[j][i],
    print ""



回答4:


As mentioned before you can use pyFiglet for creating ascii text in python.

For example:

import pyfiglet

result = pyfiglet.figlet_format("happy birthDay", font = "crazy")
print(result)


来源:https://stackoverflow.com/questions/11383466/text-to-ascii-art-generator-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!