如何使用UTF8半块在一个字符块中具有两种颜色?

2024-05-17 14:38:20 发布

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

我见过一些终端应用程序使用半块来在一个字符块中容纳两种颜色,我想用Python重新创建它,但我不知道如何在Python中实现它,因为我在网上找不到任何关于如何实际实现它的教程/说明。我现在只知道它使用了以下字符:▀ ▄ 但我不知道它是怎么变成一个的,然后转向这个█ 在终点站

From https://github.com/sharkdp/pastel/From https://www.brow.sh/

https://github.com/sharkdp/pastel/https://www.brow.sh/


Tags: httpsgithubcom应用程序终端颜色wwwsh
1条回答
网友
1楼 · 发布于 2024-05-17 14:38:20

感谢usr2564301

One color is the foreground – text – color, the other the background color. – usr2564301

这是我的最终代码(使用Pillow和ansicolors模块):

from PIL import Image
from colors import color

basewidth = 140
img = Image.open(str(input('Input File: ')))
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
img = img.convert('RGB')
width, height = img.size

for i in range(1, height, 2):
        line = ''
        for j in range(1, basewidth):
                ra, ga, ba = img.getpixel((j, i))
                rb, gb, bb = img.getpixel((j, i+1))
                line += color('\u2584', fg=(rb, gb, bb), bg=(ra, ga, ba))
        print(line)

这就是结果:

Result

我可能会将basewidth更改为检查端子宽度,而不是使用固定值

相关问题 更多 >