carmine demurtas
carmine demurtas

Reputation: 21

NameError: name 'Parser' is not defined

I am working on a project....but i got the error NameError: name 'Parser' is not defined And i really dont know how to fix it..can someone help me the error says its line 49. ill give you the code. This is the full error: Traceback (most recent call last): File "ansi.py", line 49, in args = Parser.parse_args() NameError: name 'Parser' is not defined

from PIL import Image, ImageSequence
import argparse
import sys

ESC = "\u001b"
CSI = "["
SGR_END = "m"
CHA_END = "G"
CUP_END = "H"
ED_END = "J"
RESET_CURSOR = ESC + CSI + "1;1" + CUP_END
HIDE_CURSOR = ESC + CSI + "?25l"
RESET_DISPLAY = ESC + CSI + "0" + SGR_END + ESC + CSI + "2" + ED_END


def get_rgb_escape(r: float, g: float, b: float) -> str:
    return "\u001b[48;2;{};{};{}m".format(r, g, b) + "\u001b[38;2;{};{};{}m".format(r, g, b)


def brightness(r: float, g: float, b: float) -> float:
    return 0.2126 * r + 0.7152 * g + 0.0722 * b


def image_to_text(image: Image, palette=None) -> str:
    if palette is None:
        palette = {
            64: "█",
            128: "▓",
            192: "▒",
            256: "░",
        }

    text = ""

    for y in range(image.size[1]):
        for x in range(image.size[0]):
            escape = get_rgb_escape(*image.getpixel((x, y)))
            character = next(palette[k] for k in palette.keys() if brightness(*image.getpixel((x, y))) < k)
            text += escape + character
        text += ESC + CSI + "0" + SGR_END + "\n"

    return text


if __name__ == '__main__':
    bar_length = 10
    info = "xv12"
    parser = argparse.ArgumentParser()

    args = parser.parse_args()

    image = image.open(args.input_file)

    frames = [frame.copy().convert("RGB") for frame in ImageSequence.Iterator(image)]

    frames = [frame.resize((frame.size[0] if args.width is None else args.width, frame.size[1] if args.height is None else args.height)) for frame in frames]

    text = HIDE_CURSOR + info + "\n"

    round_progress = 0

    for index, frame in enumerate(frames):
        image_text = image_to_text(frame)
        if args.show_output:
            print(RESET_CURSOR + image_text)

        text += RESET_CURSOR + image_text

        if args.show_progress:
            progress = index / len(frames)
            if round_progress != (round_progress := int(bar_length * progress + 0.5)):
                bar = "[{}{}]".format("█" * round_progress, " " * (bar_length - round_progress))
                sys.stdout.write(ESC + CSI + "1" + CHA_END + bar)
                sys.stdout.flush()

    text += RESET_DISPLAY

    if args.show_progress:
        print()

    with open(args.output_file, "wb") as f:
        f.write(bytes(text, "utf-8"))

Upvotes: 2

Views: 14146

Answers (1)

user5386938
user5386938

Reputation:

First, you need to create a parser.

parser = argparse.ArgumentParser()

Then you need to call it's parse_args method.

args = parser.parse_args()

Upvotes: 2

Related Questions