Ejemplo n.º 1
0
def output_sixel(data, im, tw, th):
    from libsixel import sixel_dither_initialize, sixel_dither_set_palette, sixel_dither_new, SIXEL_BUILTIN_G8, SIXEL_BUILTIN_G1, SIXEL_PIXELFORMAT_RGB888, SIXEL_PIXELFORMAT_RGBA8888, sixel_encode, sixel_dither_unref, sixel_output_new, sixel_dither_get, sixel_dither_set_pixelformat, SIXEL_PIXELFORMAT_PAL8, SIXEL_PIXELFORMAT_G8, SIXEL_PIXELFORMAT_G1
    s = BytesIO()
    if im.mode == 'P':
        im2 = im.convert('RGB')
        im = im2

    im.thumbnail(size=(tw, th), resample=Image.LANCZOS)
    data = im.tobytes()
    output = sixel_output_new(lambda data, s : s.write(data), s)


    if im.mode == 'RGB':
        dither = sixel_dither_new(256)
        sixel_dither_initialize(dither, data, tw, th, SIXEL_PIXELFORMAT_RGB888)
    elif im.mode == 'RGBA':
        dither = sixel_dither_new(256)
        sixel_dither_initialize(dither, data, tw, th, SIXEL_PIXELFORMAT_RGBA8888)

    elif im.mode == 'P':
        print("Its broken")
        sys.exit(1)
        palette = im.getpalette()
        print(">>", type(palette), "<<")
        dither = sixel_dither_new(256)
        print(">>", type(dither), "<< ")
        sixel_dither_set_palette(dither, palette)
        sixel_dither_set_pixelformat(dither, SIXEL_PIXELFORMAT_PAL8)
    elif im.mode == 'L':
        dither = sixel_dither_get(SIXEL_BUILTIN_G8)
        sixel_dither_set_pixelformat(dither, SIXEL_PIXELFORMAT_G8)
    elif im.mode == '1':
        dither = sixel_dither_get(SIXEL_BUILTIN_G1)
        sixel_dither_set_pixelformat(dither, SIXEL_PIXELFORMAT_G1)
    sixel_encode(data, tw, th, 1, dither, output)
    print(s.getvalue().decode('ascii'))
    sixel_dither_unref(dither)
Ejemplo n.º 2
0
def display_in_terminal(obj):
    """ Convert and display an output tensor from BigGAN in the terminal.
        This function use `libsixel` and will only work in a libsixel-compatible terminal.
        Please refer to https://github.com/saitoha/libsixel for more details.

        Params:
            obj: tensor or numpy array of shape (batch_size, channels, height, width)
            file_name: path and beggingin of filename to save.
                Images will be saved as `file_name_{image_number}.png`
    """
    try:
        import PIL
        from libsixel import (sixel_output_new, sixel_dither_new,
                              sixel_dither_initialize,
                              sixel_dither_set_palette,
                              sixel_dither_set_pixelformat, sixel_dither_get,
                              sixel_encode, sixel_dither_unref,
                              sixel_output_unref, SIXEL_PIXELFORMAT_RGBA8888,
                              SIXEL_PIXELFORMAT_RGB888, SIXEL_PIXELFORMAT_PAL8,
                              SIXEL_PIXELFORMAT_G8, SIXEL_PIXELFORMAT_G1)
    except ImportError:
        raise ImportError(
            "Display in Terminal requires Pillow, libsixel "
            "and a libsixel compatible terminal. "
            "Please read info at https://github.com/saitoha/libsixel "
            "and install with pip install Pillow libsixel-python")

    s = BytesIO()

    images = convert_to_images(obj)
    widths, heights = zip(*(i.size for i in images))

    output_width = sum(widths)
    output_height = max(heights)

    output_image = PIL.Image.new('RGB', (output_width, output_height))

    x_offset = 0
    for im in images:
        output_image.paste(im, (x_offset, 0))
        x_offset += im.size[0]

    try:
        data = output_image.tobytes()
    except NotImplementedError:
        data = output_image.tostring()
    output = sixel_output_new(lambda data, s: s.write(data), s)

    try:
        if output_image.mode == 'RGBA':
            dither = sixel_dither_new(256)
            sixel_dither_initialize(dither, data, output_width, output_height,
                                    SIXEL_PIXELFORMAT_RGBA8888)
        elif output_image.mode == 'RGB':
            dither = sixel_dither_new(256)
            sixel_dither_initialize(dither, data, output_width, output_height,
                                    SIXEL_PIXELFORMAT_RGB888)
        elif output_image.mode == 'P':
            palette = output_image.getpalette()
            dither = sixel_dither_new(256)
            sixel_dither_set_palette(dither, palette)
            sixel_dither_set_pixelformat(dither, SIXEL_PIXELFORMAT_PAL8)
        elif output_image.mode == 'L':
            dither = sixel_dither_get(SIXEL_BUILTIN_G8)
            sixel_dither_set_pixelformat(dither, SIXEL_PIXELFORMAT_G8)
        elif output_image.mode == '1':
            dither = sixel_dither_get(SIXEL_BUILTIN_G1)
            sixel_dither_set_pixelformat(dither, SIXEL_PIXELFORMAT_G1)
        else:
            raise RuntimeError('unexpected output_image mode')
        try:
            sixel_encode(data, output_width, output_height, 1, dither, output)
            print(s.getvalue().decode('ascii'))
        finally:
            sixel_dither_unref(dither)
    finally:
        sixel_output_unref(output)