Ascimage

Stable

Images as terminal cells for Go, and a terminal image viewer

Used by ViF · Built on Terminal and Color

Converts PNG, JPEG and GIF images to terminal cells, a quadrant glyph and two colors fitted to each cell or one background color a cell, and stores them as .aimg files, each cell in true color and the xterm 256 palette at once. The ascimage command converts, prints, and browses folders of images full screen.

  • A quadrant glyph and two colors a cell, the fit with the least squared error to its four samples
  • The .aimg format: every cell in true color and in 256 colors, gzipped as .aimgz
  • Converts one window of the grid, cell for cell as the whole, so a viewer converts only what it shows
  • A folder browser and full-screen viewer, zooming from 5% to 800% of 1:1

How an image becomes cells

A cell takes four samples of the image, one under the center of each of its quarters, and shows them as one of the Unicode quadrant glyphs with a foreground and a background color: of the splits of the four into two groups, the one with the least squared error. In background mode a cell is one color and a space. A cell whose four samples are all transparent stays transparent, so it draws nothing over what is beneath it. -aspect, a cell’s width over its height, 0.5 by default, keeps the image’s proportions.

ConvertRect converts one window of the grid, its cells the same as the whole image’s, so a viewer converts only the cells on screen, and the library holds no state. It imports the standard library and the terminal and color libraries alone.

The .aimg file

A short text header, the grid’s size, its mode and an anchor for its user, then 13 bytes a cell: the rune, true color foreground and background, their xterm 256 indices, and a transparency flag. A reader draws it on either kind of terminal without converting, and on 16 colors maps the true colors to the nearest ANSI ones. .aimgz is the same file gzipped. A file’s form is read from its bytes, never its name, and a reader checks the grid’s size against its limits, 16384 cells a side and 4194304 cells, before it allocates.

ViF’s image walls are made from these files, and its logo’s cells are fitted with the library.

The command

ascimage with no command browses the working folder: its subfolders and images, with a preview of the one under the cursor, beside the list or under it as the terminal’s shape allows. Enter views an image full screen, at fit or on a ladder of zooms, converting only the cells shown and decoding the images either side ahead. Both take the keyboard and the mouse, switch quadrant and background cells and the color depth, and write the marked images or the one in view.

ascimage                                      # browse the working folder
ascimage photo.jpg                            # view it, its folder behind it
ascimage convert -w 140 photo.jpg wall.aimgz  # 140 cells wide, gzipped
ascimage convert -c 256 wall.aimgz wall.ans   # ANSI text in 256 colors
ascimage print -w 60 photo.jpg | less -R
ascimage info wall.aimgz