用Python制作彩色Ascii文本

2024-05-02 08:02:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的代码制作一个带有彩色ascii码的标题,它在启动代码时会弹出。我已经知道如何制作ascii文本,但不会着色

这是我能做的,但现在我想要洋红的

result = pyfiglet.figlet_format("By Gxzs", font = "slant" )
print(result)

Ascii

我已经尝试使用colorama和termcolor自己制作,但我得到了一个奇怪的输出:

title = pyfiglet.figlet_format(colored("By  Gxzs", color = "magenta"))
print(title)

Output


Tags: 代码文本format标题bytitleasciiresult
3条回答

你可以用像

af = 132
color = subprocess.run(['tput', 'setaf', f'{af}'], capture_output=True).stdout.decode('utf-8')
sgr0 = subprocess.run(['tput', 'sgr0'], capture_output=True).stdout.decode('utf-8')
print(f'{color}HELLO{sgr0}')

可以使用https://gist.github.com/dtmilano/4055d6df5b6e4ea87c5a72dc2d604193查看颜色的值(在本例中为前景)

你可以试试:

import pyfiglet as pyfiglet

def to_color(string, color):
    color_code = {'blue': '\033[34m',
                    'yellow': '\033[33m',
                    'green': '\033[32m',
                    'red': '\033[31m'
                    }
    return color_code[color] + str(string) + '\033[0m'

result = pyfiglet.figlet_format("By Gxzs", font = "slant" )

print(to_color(result,'blue'))

有关ANSI Scape代码和颜色代码的详细信息click here

终端中“富”文本的高级库是rich。您提到的colorama库是它的依赖项之一

要以洋红色显示标题,请执行以下操作:

import pyfiglet
from rich import print

title = pyfiglet.figlet_format('By Gxzs', font='slant')
print(f'[magenta]{title}[/magenta]')

相关问题 更多 >