[![Actions Status](https://github.com/tecolicom/Text-ANSI-Image/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/tecolicom/Text-ANSI-Image/actions?workflow=test) [![MetaCPAN Release](https://badge.fury.io/pl/Text-ANSI-Image.svg)](https://metacpan.org/release/Text-ANSI-Image)
# NAME

Text::ANSI::Image - images as lines of text for the terminal

# SYNOPSIS

    use Text::ANSI::Image;

    my $img = Text::ANSI::Image->new or exit;   # no method for the terminal
    print "$_\n" for $img->rows('logo.png', width => 40);
    $img->flush;

# VERSION

Version 0.04

# DESCRIPTION

<div>
    <p><img width="602" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Image/main/images/ansipic.png"></p>
</div>

This module draws an image in a terminal as lines of text.  The lines
can be laid out, folded around, paged and scrolled like any other
text: put in columns by [ansicolumn(1)](https://metacpan.org/pod/App%3A%3Aansicolumn), shown by
`less`, and they stay with the text around them.

How the lines are made is the _method_.  Each method is a module
`Text::ANSI::Image::`_NAME_.  The one of this distribution is:

- `kitty`

    The Unicode placeholders of the kitty graphics protocol.  Each cell
    of the image is the character U+10EEEE, with its row and column as
    combining characters and the id of the image as the foreground
    color.  The image itself is sent to the terminal once, by **flush**,
    and the terminal draws it in the cells, in its full resolution.
    kitty, Ghostty and iTerm2 3.7.3 or later support it.

PNG, JPEG and GIF files are shown.  The type is detected from the
content, not the name of the file.  The `kitty` method sends PNG; a
JPEG or a GIF is converted to PNG by `sips` (macOS) or ImageMagick,
and is not shown without them.  A PNG much larger than the room is
reduced by the same tools to send less data, or sent as it is without
them.

# METHODS

- **new**(_OPTIONS_)

    Returns an object of a method.  The options are:

    - **method** => _NAME_

        The method: `kitty`, or `auto` (the default), the first method in
        `@Text::ANSI::Image::METHODS` which the terminal supports.  With
        `auto`, **new** returns `undef` when the terminal supports none.  An
        unknown name dies.

    - **term** => _COLS_**x**_ROWS_\[**:**_W_**x**_H_\]

        The size of the terminal, in columns and rows, and the size of a cell
        in pixels.  By default it is read from `/dev/tty`.  A cell of 8x17
        pixels is taken when its size is not known.  The size of a cell not
        given here is **TERM\_CELLSIZE** when it is set; see ["ENVIRONMENT"](#environment).

    - **tty** => _FILE_

        Where the data of the images is sent by **flush** (default:
        `/dev/tty`).

    - **width** => _N_, **rows** => _N_

        The most columns and rows that an image may take (default: the
        terminal width, and its height but four lines).

    - **types** => \[ _TYPE_, ... \]

        The types of images shown: `png`, `jpeg` and `gif` (default: all).

    - **osc** => _BOOL_

        Put a private OSC at the beginning of each row, which tells the id of
        the image, its size in cells and the absolute path of the file, for a
        program which draws the image itself where its cells are, as
        [ansi2pdf](https://github.com/tecolicom/App-ansi2pdf) does (default:
        off; `kitty` only).  Terminals ignore it:

            ESC ] 7771 ; i=ID,c=COLS,r=ROWS ; FILE ESC \

    - **resend** => _FILE_

        **flush** also appends to _FILE_ the sequences which send the images
        again by the paths of their files, which the terminal reads: the file
        of the image as it is (`t=f`), or the PNG made from it, a temporary
        file which is not removed and which the terminal removes after
        reading it (`t=t`) (`kitty` only).  They are short, so that another
        program can send them later: as the initialization string of **less**
        (`LESS_TERMCAP_ti`), after it switches to the alternate screen, where
        the images sent before are not seen in kitty and Ghostty, which keep
        them for each screen.  The terminal must be on the same host.

- **rows**(_FILE_, _OPTIONS_)

    Returns the rows of the image of _FILE_, as lines without newlines,
    or an empty list when it is not shown: the file is not found, not an
    image, of a type not in **types**, or cannot be converted.  **width**,
    **rows** and **types** may be given for this call.  An image is as large
    as its pixels, and not larger than the room.

    **size** => \[ _W_, _H_ \] gives the size of the image in points,
    as the `width` and `height` of an HTML `<img>`: one of them
    alone (the other `undef` or 0) keeps the aspect ratio.  The image is
    sent as it is, and the terminal fits it to the cells, so that an image
    larger in pixels is drawn finer on a Retina display.

- **flush**

    Sends to the terminal what the rows given so far need: the images
    for `kitty`.  Call it before the rows are shown, or soon after.

- **method**

    The name of the method of the object.

- **supported**

    Whether the terminal supports the method, by the environment.  It is
    a class method: `Text::ANSI::Image::kitty->supported`.

- **terminal**

    Columns, rows and the size of a cell in pixels of the terminal.  As a
    class method, it takes _term_ as the argument:
    `Text::ANSI::Image->terminal` reads the size of `/dev/tty`.

- **info**(_FILE_)

    Width, height and type of an image file, or an empty list.  It is a
    class method too.

- **cells**(_W_, _H_, _OPTIONS_)

    The columns and rows that an image of _W_ x _H_ pixels takes.

# METHOD MODULES

A method is a module `Text::ANSI::Image::`_NAME_, a subclass of
Text::ANSI::Image, which has:

- **render**(_FILE_, _W_, _H_, _TYPE_, _COLS_, _ROWS_)

    The rows of the image in _COLS_ x _ROWS_ cells.  Required.

- **supported**

    Whether the terminal supports it (default: no).

- **max\_cells**

    The most columns and rows it can draw (default: 0, no limit).

- **flush**

    What is sent to the terminal (default: nothing).

**png** is there for a method which needs a PNG of the image.

# ENVIRONMENT

- **TERM\_CELLSIZE**=_W_**x**_H_

    The size of a cell, in the pixels of an image, in place of the size
    which the terminal tells or which is asked of it.  The numbers may
    have fractions, as `12x24.5`.  The size given by **term** is taken
    over it.

    Without it, the terminal is asked for the size of a cell in points
    by `ReportCellSize` of iTerm2, once in a process, and a point is
    taken as two pixels of an image (`$Text::ANSI::Image::DENSITY`), as
    an image of high resolution (`2x` of `srcset` of HTML): it is shown
    as it is on a Retina display and thinned out on others, and takes the
    same cells on both.  The size of an image in points can be given to
    **rows** by **size**, as the `width` and `height` of an HTML
    `<img>`.  The terminal is asked, and the answer
    waited for half a second at most, less when it answers the request of
    the device attributes which follows.  Other terminals are not known
    to tell the size in points; then the size of a cell in the pixels of
    the screen is taken, which a terminal counts twice as many on a
    Retina display as on others, so that an image takes half as many
    cells there.

    The variable saves asking the terminal, or gives a size where it
    cannot be asked.

# FUNCTIONS

- **shellenv**

    Prints the lines which set the variables of ["ENVIRONMENT"](#environment) in a
    shell, for `eval`, and returns true; a variable which is not known is
    left out, so that it is not set, and nothing is printed and false
    returned when none is known.  In a shell, so that the terminal is not
    asked by each command:

        eval "$(perl -MText::ANSI::Image=shellenv -e shellenv)"

    It prints `export TERM_CELLSIZE=`_W_**x**_H_, the size of a cell in
    the pixels of an image: the size in points which the terminal tells
    (iTerm2), or else, on macOS, the size in the pixels of the screen
    which the tty tells divided by the scale of the main screen, which is
    asked of macOS by `osascript` (a tenth of a second), both times
    `$Text::ANSI::Image::DENSITY`: the same size on a Retina display and
    on others, in any terminal which tells the size of its window in
    pixels.  The scale is of the screen when it is run, so run it on the
    screen where the terminal is used.

# SEE ALSO

[Terminal graphics protocol, Unicode placeholders](https://sw.kovidgoyal.net/kitty/graphics-protocol/#unicode-placeholders)

[App::mdee](https://metacpan.org/pod/App%3A%3Amdee), which shows the images of Markdown files with it.

[App::ansicolumn](https://metacpan.org/pod/App%3A%3Aansicolumn), [Text::ANSI::Fold](https://metacpan.org/pod/Text%3A%3AANSI%3A%3AFold)

# AUTHOR

Kazumasa Utashiro

# LICENSE

Copyright © 2026 Kazumasa Utashiro.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
