Windows -有效地将大量数据打印到终端



TL;DR寻找一种加速打印到终端的方法。

我目前正在做一个项目,基本上可以让您在终端中使用ANSI转义码打印图像,(需要缩小以获得最佳效果,所以我建议使用新的windows终端)

这是代码:

try:
from PIL import Image
except:
from Lib.PIL import Image
from time import time
from os import system
from sys import argv, stdout
from pathlib import Path

def imgToTxt(path: Path, x_ratio: int, y_ratio: int, pixel: str, do_bg: bool, stat: bool, do_prnt: bool, do_warp: bool):
# COLOR FUNCTIONS
fg = lambda c: f"x1b[38;2;{str(c[0])};{str(c[1])};{str(c[2])}m"
bg = lambda c: f"x1b[48;2;{str(c[0])};{str(c[1])};{str(c[2])}m"
# /COLOR FUNCTIONS
st = time()
prnt_pixels = list()
_c = False
# IMAGE DATA
im = Image.open(path)
pixels = im.load()
width, height = im.size
# / IMAGE DATA
if stat:
print(height, y_ratio)
print(width, x_ratio)
system("")
# GENERATION LOOP
for y in range(0, height, y_ratio):
if do_warp:
if _c:
_c = False; continue
else:
_c = True
for x in range(0, width, x_ratio):
pu = pixels[x, y]
pl = pixels[x, y + 1 if y + 1 < height else y]
p = fg(pu) + ((bg(pl) if do_warp else bg(pu)) if do_bg else "") + pixel + "x1b[0m"
prnt_pixels.append(p)
else:
prnt_pixels.append("n")
# /GENERATION LOOP
# PRINTING
if do_prnt:
for p in prnt_pixels:
stdout.write(p)
# /PRINTING
nd = time()
print(("time to generate", nd - st) if stat else "")
return prnt_pixels

if __name__ == '__main__':
argv.append("-run")
argv.append("True")
if "help" in argv[1]:
print(
""" Convert an image into text
r    line commands:
r        -path [PATH]    -- [PATH] full path to the image
r        -xr [INT]       -- [INT] number between 1 and size of image, determines the x ratio (1 being raw)
r        -yr [INT]       -- [INT] number between 1 and size of image, determines the y ratio (1 being raw)
r        -pix [STR]      -- [STR] string of length 2, determines the pixel to use, default 'OO'
r        -do_bg [BOOL]   -- [BOOL] boolean, determines if the background has to be filled, default 'True'
r        -do_prnt [BOOL] -- [BOOL] boolean, determines if the image will be displayed, default 'True'
r        -do_warp [BOOL] -- [BOOL] boolean, determines if to warp the image in return for higher quality, default 'False', WARNING this option will force the change of default pixel
r        -help OR /help  -- Display this message""")
else:
# ALL
if len(argv) == 1:
argv = input(r'args >').split(" ")
# /ALL
# SET TO NEXT VALUE
if "-path" in argv:
_ = argv.index("-path") + 1
path = Path(argv[_])
else:
path = r"C:\Windows\Cursors\aero_unavail_xl.cur"
if "-xr" in argv:
_ = argv.index("-xr") + 1
x_ratio = int(argv[_])
else:
x_ratio = 1
if "-yr" in argv:
_ = argv.index("-yr") + 1
y_ratio = int(argv[_])
else:
y_ratio = 1
if "-pix" in argv:
_ = argv.index("-pix") + 1
pix = argv[_]
else:
pix = "00"
# /SET TO NEXT VALUE
# TRUE | FALSE
if "-do_bg" in argv:
_ = argv.index("-xr") + 1
match argv[_].lower():
case 'true':
do_bg = True
case 'false':
do_bg = False
case _:
raise Exception("-do_bg takes only true/false statements")
else:
do_bg = True
if "-do_warp" in argv:
_ = argv.index("-do_warp") + 1
match argv[_].lower():
case 'true':
do_warp = True
pix = '▀'
case 'false':
do_warp = False
case _:
raise Exception("-do_warp takes only true/false statements")
else:
do_warp = False
if "-do_prnt" in argv:
_ = argv.index("-do_prnt") + 1
match argv[_].lower():
case 'true':
do_prnt = True
case 'false':
do_prnt = False
case _:
raise Exception("-do_prnt takes only true/false statements")
else:
do_prnt = True
if "-stat" in argv:
_ = argv.index("-stat") + 1
match argv[_].lower():
case 'true':
stat = True
case 'false':
stat = False
case _:
raise Exception("-stat takes only true/false statements")
else:
stat = True
if "-run" in argv:
_ = argv.index("-run") + 1
match argv[_].lower():
case 'true':
run = True
case 'false':
run = False
case _:
raise Exception("-run takes only true/false statements")
else:
run = False
# /TRUE | FALSE
if run:
imgToTxt(path=path, x_ratio=x_ratio, y_ratio=y_ratio, pixel=pix, do_bg=do_bg, do_prnt=do_prnt, do_warp=do_warp, stat=stat)

它的基本工作方式是获取图像的每个像素颜色数据,并为颜色准备正确的ANSI代码;ESC[38;2;{R};{G};{B}m
,然后在后期打印它

我一直在研究一种加快印刷过程的方法IE,使用stdout,使用单独的循环来生成数据并打印它,我甚至尝试使用多个线程(我知道这听起来有多愚蠢…),使用替代缓冲区,但最终我得到了大部分分数的速度提高。

有没有一种方法,即使是疯狂的方法,可以显著加快这个过程?

问题在于您应该缓冲您的输出,而不是为每个字符编写自己的字符并等待它(这里描述)。这里也有一些解释

我没有拿走你的全部代码,但是你应该尽可能多地打印。改变这个:

if do_prnt:
for p in prnt_pixels:
stdout.write(p)

变成这样:

if do_prnt:
stdout.write("".join(prnt_pixels))

你必须在打印之前用空字符串连接你的数据,因为你有一个列表,但你只想打印列表的数据,而不是额外的列表内容。

此外,您应该查看argparse进行参数解析

每次打印一个字符。你必须一次打印全部:

stdout.write(prnt_pixels)

相关内容

  • 没有找到相关文章

最新更新