Home > Software engineering >  Windows - Printing Large amounts of data to the terminal effectively
Windows - Printing Large amounts of data to the terminal effectively

Time:10-31

TL;DR looking for a way to speed up printing to terminal.

i am currently working on a project that basically lets you print out images in the terminal using ANSI escape codes, (requires zooming out for best results so i recommend using the new windows terminal)

This is the code:

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)

the basic way it works is that it gets each pixel color data of an image and prepares a correct ANSI code for the color; ESC[38;2;{R};{G};{B}m
then in a later stage printing it

i have been looking into a way to speed up the printing process IE, using stdout, using a separate loop to generate the data and to print it, i even tried to use multiple threads (i know how stupid that sounds...), using alternative buffer, but in the end i got mostly fractions of a speed increase.

is there any way, even a crazy one, that would significantly speed up this process?

CodePudding user response:

The problem is that you should buffer your output to not write each character for its own and wait for it (described here). Some explanations are also here

I did not took your full code, but you should print as much as possible at same time. change this:

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

to something like this:

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

You have to join your data with empty string before printing, because you have a list, but you want to print only the data of the list and no additional list stuff.

Additionally you should have a look at argparse for argument parsing

CodePudding user response:

You are printing a character at a time. You have to print all at once:

stdout.write(prnt_pixels)
  • Related