Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active January 8, 2024 16:42
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CMCDragonkai/63705b7ea09199ffe385f97bce358cdb to your computer and use it in GitHub Desktop.
Save CMCDragonkai/63705b7ea09199ffe385f97bce358cdb to your computer and use it in GitHub Desktop.
CLI: View Images on Command Line

View Images on Command Line

When working with plotting like gnuplot or ggplot2 or just navigating your filesystem on the command line, it would be nice to be able to view bitmap or vector graphics easily.

This is all for the ideal of hybrid GUI TUI interface!!

However this turns out to be difficult. However I found pretty much 5 ways to do this.

SIXEL

If your terminal emulator can handle SIXEL graphics, it can display SIXEL graphics inline: https://github.com/saitoha/libsixel (you can convert anything to SIXEL including SVG)

This would be most preferable, as it was a protocol designed to show graphics on the terminal. Which means it can work over the network (like SSH).

ASCII Armor

ASCII armor your images! This is the most portable. Use cacaview or hiptext.

w3imgdisplay

You have to use the latest w3m with support for this compiled in. Then you need a terminal emulator that w3mimgdisplay supports. I've only found xterm and gnome-terminal to support this. It's basically a hack, and it still relies on X. That is the images are displayed inline inside the terminal emulator, but it's actually terminal graphics, it's just overlays X graphics on top. So it cannot be used over SSH.

Ranger makes use of this to allow previewing of images when navigating the filesystem.

Using X and piping images to an image viewer

Ok this isn't really using the terminal emulator, but the workflow is pretty solid. Basically do this:

# scale-down scales down large images
# auto-zoom scales up small images
# keep-zoom-vp is required if there very different sized images, we don't want the window jumping up and down
# magick-timeout allows using imagemagick to lookup SVG files
alias feh='feh \
           --scale-down \
           --auto-zoom \
           --borderless \
           --image-bg black \
           --draw-filename \
           --draw-tinted \
           --keep-zoom-vp \
           --magick-timeout 1'

cat image.jpeg | feh -
feh ./directory/with/images

It works best using a tiling windows manager. You can hook your windows manager like XMonad to display feh windows in a floating manner. Otherwise, it's going to take focus away from your command prompt, and that's annoying.

-- excerpt from my xmonad.hs
matrixHooks = 
    composeAll [
        className =? "feh" --> doCenterFloat
    ] 

You can also run such a command on dmenu prompt. Allowing you to fill up the entire screen with multi window images.

feh is pretty powerful, it has lots of keyboard shortcuts that you can use once you have the image opened up. Note that the --magick-timeout option is only useful if you have imagemagick, it allows you to view SVG files as well.

Using a better system

Like Emacs or Jupyter or iTerm2 or BlackScreen.

Or wait for some epic revolution to terminal emulators that derives from the conversational UI trend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment