Search code examples
pythonasciimatics

tmux - how to display an image in a pane?


I'd like to display an animated GIF or any other image in a pane in tmux.

I'm playing with asciimatics to do this, and have modded one of the sample programs (images.py) to:

  • display a single image
  • show no error messages on "Ctrl+C"
  • accept a single command line arg "image filename"

Here's the script I have and the only issue is that it seems to do a slow refresh of the image being displayed ~ every 10 seconds. How do I remove this refresh, since the image is a static image?

image.py

from __future__ import division
from asciimatics.effects import BannerText, Print, Scroll
from asciimatics.renderers import ColourImageFile, FigletText, ImageFile
from asciimatics.scene import Scene
from asciimatics.screen import Screen
from asciimatics.exceptions import ResizeScreenError
import sys

total = len(sys.argv)-1
if (total < 1):
    print ("Usage: IMG")
    sys.exit(1)

# Parsing args one by one
IMG = str(sys.argv[1])

def demo(screen):
    scenes = []
    effects = [
        Print(screen,
              ColourImageFile(
                  screen, IMG, 
                  screen.height-2,
                  uni=screen.unicode_aware,
                  dither=screen.unicode_aware),
                  0,
                  stop_frame=200
            )
    ]
    scenes.append(Scene(effects))
    screen.play(scenes, stop_on_resize=True)


# capture ctrl+c and exit nicely
import signal
import sys
def signal_handler(sig, frame):
        sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)

if __name__ == "__main__":
    while True:
        try:
            Screen.wrapper(demo)
            #Screen.wrapper(demo, catch_interrupt=True)
            sys.exit(0)
        except ResizeScreenError:
            sys.exit(0)

Solution

  • I just had to set stop_frame=0 and it works as hoped, as specified in the docs

    Example usage

    $ python image.py /images/fox.jpg
    

    image.py

    from __future__ import division
    from asciimatics.effects import BannerText, Print, Scroll
    from asciimatics.renderers import ColourImageFile, FigletText, ImageFile
    from asciimatics.scene import Scene
    from asciimatics.screen import Screen
    from asciimatics.exceptions import ResizeScreenError
    import sys
    
    total = len(sys.argv)-1
    if (total < 1):
        print ("Usage: IMG")
        sys.exit(1)
    
    # Parsing args one by one
    IMG = str(sys.argv[1])
    
    def demo(screen):
        scenes = []
        effects = [
            Print(screen,
                  ColourImageFile(
                      screen, IMG, 
                      screen.height-2,
                      uni=screen.unicode_aware,
                      dither=screen.unicode_aware),
                      0,
                      stop_frame=200
                )
        ]
        scenes.append(Scene(effects))
        screen.play(scenes, stop_on_resize=True)
    
    
    # capture ctrl+c and exit nicely
    import signal
    import sys
    def signal_handler(sig, frame):
            sys.exit(0)
    signal.signal(signal.SIGINT, signal_handler)
    
    if __name__ == "__main__":
        while True:
            try:
                Screen.wrapper(demo)
                #Screen.wrapper(demo, catch_interrupt=True)
                sys.exit(0)
            except ResizeScreenError:
                sys.exit(0)