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,使用單獨的回圈來生成資料并列印它,我什至嘗試使用多個執行緒(我知道這聽起來多么愚蠢......),使用替代緩沖區,但最終我獲得了大部分速度提升。
有什么辦法,即使是一個瘋狂的辦法,可以顯著加快這個程序?
uj5u.com熱心網友回復:
問題是你應該緩沖你的輸出而不是為它自己寫每個字符并等待它(描述here)。一些解釋也在這里
我沒有拿走你的完整代碼,但你應該盡可能多地同時列印。改變這個:
if do_prnt:
for p in prnt_pixels:
stdout.write(p)
像這樣:
if do_prnt:
stdout.write("".join(prnt_pixels))
您必須在列印前用空字串連接資料,因為您有一個串列,但您只想列印串列中的資料,而不想列印其他串列內容。
此外,您應該查看argparse以進行引數決議
uj5u.com熱心網友回復:
您一次列印一個字符。您必須一次列印所有內容:
stdout.write(prnt_pixels)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/346540.html
