Beispiel #1
0
def draw_image(img):
    width, height = img.size
    sys.stdout.write("\x1b[49m\x1b[K")
    sys.stdout.write(
        ansi.generate_ANSI_from_pixels(img.load(), width, height, None)[0])
    sys.stdout.write("\x1b[0m\n")
    sys.stdout.flush()
Beispiel #2
0
    def generate(bytesio):
        assert isinstance(bytesio, io.BytesIO)
        for frame in looper(process_image(bytesio), 3):
            img = resize_image(frame,
                               antialias=False,
                               maxLen=50.0,
                               aspectRatio=1.0)
            pixel = img.load()
            width, height = img.size

            yield CSI_SGR_BG + CSI_ERASE + \
                ansi.generate_ANSI_from_pixels(pixel, width, height, None)[0]
            time.sleep(0.05)

        yield CSI_SGR_BG + CSI_ERASE + CSI_SGR_RESET + "\n"
Beispiel #3
0
def img_parse(img_url, avatar=False):
    file = cStringIO.StringIO(urllib.urlopen(img_url).read())
    remote_img = Image.open(file)
    if avatar is True:
        img = img2txt.load_and_resize_image(file, True, 20, 1.0)
    else:
        img = img2txt.load_and_resize_image(file, True,
                                            (get_terminal_width() / 3) * 2,
                                            1.0)
    pixel = img.load()
    width, height = img.size
    sys.stdout.write("\x1b[49m")
    sys.stdout.write("\x1b[K")
    sys.stdout.write(
        ansi.generate_ANSI_from_pixels(pixel, width, height, None)[0])
    sys.stdout.write("\x1b[0m\n")
Beispiel #4
0
def nextFrame(imgOrig, frame, newWidth, newHeight):
    imgOrig.seek(frame)
    img = imgOrig
    if img.mode != 'RGBA':
        img = img.convert('RGBA')
    # antialias = Image.ANTIALIAS
    antialias = Image.NEAREST
    img = img.resize((newWidth, newHeight), antialias)
    pixel = img.load()
    width, height = img.size

    # output frame
    fill_string = "\x1b[49m"
    fill_string += "\x1b[K"
    sys.stdout.write(fill_string)
    sys.stdout.write(
        ansi.generate_ANSI_from_pixels(pixel, width, height, None)[0])
    sys.stdout.write("\x1b[0m\n")

    # frame separator
    sys.stdout.write('ZF_img2txt_ZF\n')
Beispiel #5
0
            # supports BCE (Background Color Erase) otherwise we're going to
            # get the default bg color regardless. If a terminal doesn't
            # support BCE you can output spaces but you'd need to know how many
            # to output (too many and you get linewrap)
            fill_string = ansi.getANSIbgstring_for_ANSIcolor(
                ansi.getANSIcolor_for_rgb(bgcolor))
        else:
            # reset bg to default (if we want to support terminals that can't
            # handle this will need to instead use 0m which clears fg too and
            # then when using this reset prior_fg_color to None too
            fill_string = "\x1b[49m"
        fill_string += "\x1b[K"          # does not move the cursor
        sys.stdout.write(fill_string)

        sys.stdout.write(
            ansi.generate_ANSI_from_pixels(pixel, width, height, bgcolor)[0])

        # Undo residual color changes, output newline because
        # generate_ANSI_from_pixels does not do so
        # removes all attributes (formatting and colors)
        sys.stdout.write("\x1b[0m\n")
    else:

        if clr:
            # TODO - should handle bgcolor - probably by setting it as BG on
            # the CSS for the pre
            string = generate_HTML_for_image(pixel, width, height)
        else:
            string = generate_grayscale_for_image(
                pixel, width, height, bgcolor)
Beispiel #6
0
            # supports BCE (Background Color Erase) otherwise we're going to
            # get the default bg color regardless. If a terminal doesn't
            # support BCE you can output spaces but you'd need to know how many
            # to output (too many and you get linewrap)
            fill_string = ansi.getANSIbgstring_for_ANSIcolor(
                ansi.getANSIcolor_for_rgb(bgcolor))
        else:
            # reset bg to default (if we want to support terminals that can't
            # handle this will need to instead use 0m which clears fg too and
            # then when using this reset prior_fg_color to None too
            fill_string = "\x1b[49m"
        fill_string += "\x1b[K"  # does not move the cursor
        sys.stdout.write(fill_string)

        sys.stdout.write(
            ansi.generate_ANSI_from_pixels(pixel, width, height, bgcolor)[0])

        # Undo residual color changes, output newline because
        # generate_ANSI_from_pixels does not do so
        # removes all attributes (formatting and colors)
        sys.stdout.write("\x1b[0m\n")
    else:

        if clr:
            # TODO - should handle bgcolor - probably by setting it as BG on
            # the CSS for the pre
            string = generate_HTML_for_image(pixel, width, height)
        else:
            string = generate_grayscale_for_image(pixel, width, height,
                                                  bgcolor)
Beispiel #7
0
def main(imgname):
    maxLen = 150
    clr = False
    do_ansi = False
    fontSize = None
    bgcolor = None
    antialias = False
    dither = False
    target_aspect_ratio = None
    try:
        maxLen = float(maxLen)
    except:
        maxLen = 100.0  # default maxlen: 100px

    try:
        fontSize = int(fontSize)
    except:
        fontSize = 7

    try:
        # add fully opaque alpha value (255)
        bgcolor = HTMLColorToRGB(bgcolor) + (255, )
    except:
        bgcolor = None

    try:
        target_aspect_ratio = float(target_aspect_ratio)
    except:
        target_aspect_ratio = 1.0  # default target_aspect_ratio: 1.0

    try:
        img = load_and_resize_image(imgname, antialias, maxLen,
                                    target_aspect_ratio)
    except IOError:
        exit("File not found: " + imgname)

    # Dither _after_ resizing
    if dither:
        img = dither_image_to_web_palette(img, bgcolor)

    # get pixels
    pixel = img.load()

    width, height = img.size

    if do_ansi:

        # Since the "current line" was not established by us, it has been
        # filled with the current background color in the
        # terminal. We have no ability to read the current background color
        # so we want to refill the line with either
        # the specified bg color or if none specified, the default bg color.
        if bgcolor is not None:
            # Note that we are making the assumption that the viewing terminal
            # supports BCE (Background Color Erase) otherwise we're going to
            # get the default bg color regardless. If a terminal doesn't
            # support BCE you can output spaces but you'd need to know how many
            # to output (too many and you get linewrap)
            fill_string = ansi.getANSIbgstring_for_ANSIcolor(
                ansi.getANSIcolor_for_rgb(bgcolor))
        else:
            # reset bg to default (if we want to support terminals that can't
            # handle this will need to instead use 0m which clears fg too and
            # then when using this reset prior_fg_color to None too
            fill_string = "\x1b[49m"
        fill_string += "\x1b[K"  # does not move the cursor
        sys.stdout.write(fill_string)

        sys.stdout.write(
            ansi.generate_ANSI_from_pixels(pixel, width, height, bgcolor)[0])

        # Undo residual color changes, output newline because
        # generate_ANSI_from_pixels does not do so
        # removes all attributes (formatting and colors)
        sys.stdout.write("\x1b[0m\n")
    else:

        if clr:
            # TODO - should handle bgcolor - probably by setting it as BG on
            # the CSS for the pre
            string = generate_HTML_for_image(pixel, width, height)
        else:
            string = generate_grayscale_for_image(pixel, width, height,
                                                  bgcolor)

        sys.stdout.write(string)

    sys.stdout.flush()
    return string