Skip to content

Instantly share code, notes, and snippets.

@hackerb9
Created June 3, 2017 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hackerb9/559bda4b8a13d4ebbc378e4c400f77ce to your computer and use it in GitHub Desktop.
Save hackerb9/559bda4b8a13d4ebbc378e4c400f77ce to your computer and use it in GitHub Desktop.
lsix: like 'ls' but shows images within a terminal using SIXEL graphics
#!/bin/bash
# lsix: like ls, but for images.
# Shows thumbnails of images with titles directly in terminal.
# Version 1.0
# hackerb9, June 2017
numcolors=16 # Number of colors in the palette, if TERM is not xterm
cleanup() {
echo $'\e\\' # Escape sequence to stop SIXEL
exit 0
}
trap cleanup SIGINT SIGHUP SIGABRT
if [[ "$TERM" =~ xterm ]]; then
# Attempt to set the number of colors to 1024.
# This will work for xterm, but fail on a real vt340.
IFS=";" read -a REPLY -s -t 0.1 -d "S" -p $'\e[?1;3;1024S'
[[ ${REPLY[1]} == "0" ]] && numcolors=${REPLY[2]}
fi
if [[ $# == 0 ]]; then
# No command line args? Use a sorted list of image files in CWD
shopt -s nullglob nocaseglob
set - *{jpg,jpeg,png,gif,tiff,tif,p?m,x[pb]m,bmp,ico,svg,eps}
[[ $# != 0 ]] || exit
mapfile -t < <(printf "%s\n" "$@" | sort)
set - "${MAPFILE[@]}"
fi
if [[ $# -le 21 ]]; then
# Only a few pictures to show, do it in one chunk.
montage -tile 7x1 -label %f -background black -fill white "$@" gif:- \
| convert - -colors $numcolors sixel:-
else
# Lots of pictures, so show them a row at a time instead
# of taking a long time to make one huge montage.
while [ $# -gt 0 ]; do
while [ $# -gt 0 -a ${#onerow[@]} -lt 7 ]; do
len=${#onerow[@]}
onerow[$len]="$1"
shift
done
montage -tile 7x1 -label %f \
-background black -fill white \
"${onerow[@]}" gif:- \
| convert - -colors $numcolors sixel:-
onerow=()
done
fi
######################################################################
# NOTES:
# * Usage: lsix [ FILES ... ]
# * FILES can be any image file that ImageMagick can handle.
# * If no FILES are specified the most common file extensions are used.
# * Non-bitmap graphics often work fine (.svg, .eps, .pdf, .xcf).
# * Because this uses escape sequences, it works seamlessly through ssh.
# * Only dependency is ImageMagick (apt-get install imagemagick).
# Your terminal must support SIXEL graphics. E.g.,
#
# xterm -ti vt340
# * To make vt340 be the default xterm type, set this in .Xresources:
#
# ! Allow sixel graphics. (Try: "convert -colors 16 foo.jpg sixel:-").
# xterm*decTerminalID : vt340
# * Be cautious using lsix on videos (lsix *.avi) as ImageMagick will
# try to make a montage of every single frame and likely exhaust
# your memory and/or your patience.
# BUGS
# * Directories are not handled nicely.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment