From b261f5c3fe4c8f58319177818d81cf2b6e313ffd Mon Sep 17 00:00:00 2001 From: Santiago Larrain E Date: Wed, 26 Jan 2022 19:19:57 -0300 Subject: [PATCH] Add support for Kitty's Terminal graphics protocol While the added support for sixel is a neat addition, there are many terminals that do not support it. Kitty is one of them. It supports it own procol for terminal graphics. This commit adds support for that protocol to rainbowstream. --- rainbowstream/c_image.py | 24 ++++++++++++++++++------ setup.py | 3 ++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/rainbowstream/c_image.py b/rainbowstream/c_image.py index 29f26a6..bafed8f 100644 --- a/rainbowstream/c_image.py +++ b/rainbowstream/c_image.py @@ -84,12 +84,9 @@ def image_to_display(path, start=None, length=None): height = min(height, c['IMAGE_MAX_HEIGHT']) - # Sixel - if c['IMAGE_ON_TERM'] == 'sixel': - import fcntl, struct, termios - from io import BytesIO - from libsixel import sixel_dither_new, sixel_dither_initialize, sixel_encode, sixel_output_new, SIXEL_PIXELFORMAT_RGBA8888 - from resizeimage import resizeimage + if c['IMAGE_ON_TERM']: # If it is either 'sixel' or True + import fcntl, struct, termios # Moved up because max_*_pixels is needed + # for both sixel and kitty # FIXME: rows and columns are gotten a second time. Maybe use this at # the begining of function instead of the call to stty size @@ -100,6 +97,21 @@ def image_to_display(path, start=None, length=None): max_width_pixels = width * (xpixels // columns) max_height_pixels = height * (ypixels // rows) + + # Kitty + if c['IMAGE_ON_TERM'] and os.environ['TERM'] == 'xterm-kitty': + from pixcat import Image as Image2 + + pic = Image2(path) + pic = pic.resize(max_w=max_width_pixels, max_h=max_height_pixels) + pic.show(align='center') + + # Sixel + elif c['IMAGE_ON_TERM'] == 'sixel': + from io import BytesIO + from libsixel import sixel_dither_new, sixel_dither_initialize, sixel_encode, sixel_output_new, SIXEL_PIXELFORMAT_RGBA8888 + from resizeimage import resizeimage + # FIXME: This way is preferable to avoid an addition dependency, but it doesn't work correctly # i = i.resize((max_width_pixels, max_height_pixels), Image.ANTIALIAS) i = resizeimage.resize_thumbnail(i, [max_width_pixels, max_height_pixels]) diff --git a/setup.py b/setup.py index 3b43d28..4b1fed1 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,8 @@ "PySocks", "pocket", "libsixel-python", - "resize-image" + "resize-image", + "pixcat" ] # Default user (considers non virtualenv method)