Home

Awesome

Terminal Image Viewer (tiv)

Small C++ program to display images in a (modern) terminal using RGB ANSI codes and unicode block graphic characters.

There are various similar tools (such as timg) that use the unicode half block character to display two 24bit pixels per character cell. This program enhances the resolution by mapping 4x8 pixel cells to different unicode characters, using the following algorithm for each 4x8 pixel cell of the (potentially downscaled) image:

  1. Find the color channel (R, G or B) that has the biggest range of values for the current cell
  2. Split this range in the middle and create a corresponding bitmap for the cell
  3. Compare the bitmap to the assumed bitmaps for various unicode block graphics characters
  4. Re-calculate the foreground and background colors for the chosen character.

See the difference by disabling this optimization using the -0 option. Or just take a look at the comparison image at the end of this text.

Usage

tiv [options] <filename> [<filename>...]

The shell will expand wildcards. By default, thumbnails and file names will be displayed if more than one image is provided. For a list of options, run the command without any parameters or with --help.

News

Installation

[!IMPORTANT] All installation methods require installing ImageMagick, a required dependency. Most package managers should install it automatically.

All platforms: Build from source

Our makefile currently only supports g++. It should be possible to compile tiv manually using any of your favorite compilers that support C++17 and Unix headers (ioctl.h and sysexits.h, specifically) or windows.h. PRs are welcome.

git clone https://github.com/stefanhaustein/TerminalImageViewer.git
cd TerminalImageViewer/src
make

# To move the tiv binary into your PATH (hopefully), also do
sudo make install

Please don't forget to install ImageMagick... On Debian based Linux via sudo apt install imagemagick and on MacOS via brew install imagemagick.

Mac: Homebrew

brew install tiv

As the original Apple Shell only supports 256 color mode (-256) and there seems to be some extra line spacing, distorting the image, we also recommend installing iTerm2:

brew install --cask iterm2

Third-Party Packages

Common problems / Troubleshooting

Using the TIV API

Tiv can be used as an API. So if you always wanted to run your favorite FPS in a shell, this is the opportunity.

All the code useful as a library is isolated in tiv_lib.h and tiv_lib.cc.

The main entry point is

CharData findCharData(GetPixelFunction get_pixel, int x0, int y0, const int &flags)

The call takes a std::Function that allows the TIV code to request pixels from your framebuffer.

From this framebuffer, the call will query pixels for a 4x8 pixel rectangle, where x0 and y0 define the top left corner. The call searches the best unicode graphics character and colors to approximate this cell of the image, and returns these in a CharData struct.

Contributions

I am happy to accept useful contributions under the Apache 2.0 license, but...

Examples

Most examples were shot with the Java version of this program, which should have equivalent output but slower by millenia in CPU years.

Examples

If multiple images match the filename spec, thumbnails are shown.

Thumbnails

For the example below, the top image was generated with the character optimization disabled via the -0 option.

Comparison

Licensing

You are free to use this code under either the GPL (3 or later) or version 2.0 of the Apache license. We include the CImg library, which is licensed under either CeCILL 2.0 (close to GPL and compatible with it) or [CeCILL-C] (https://spdx.org/licenses/CECILL-C) (close to LGPL and compatible with Apache).