Skip to content

Asthestarsfalll/img2art

Repository files navigation

Convert image/gif/video to ascii art. If you inputs have plenty of frames, you can specify --fast to use torch accelerating the peocess.

Screen Shot

example example1

Experimental feature

Now you can add --alpha to generate neovim highlight definitions, for example:

img2art xxx.png --scale 0.1 --threshold 120 --save-raw ./xxx.lua --alpha

It will generate code definitions in target file which consists of two parts:

  1. Code of setting neovim highlight like vim.api.nvim_set_hl(0, "I2A0", { fg="#2f3651" }), which will be plenty of lines.
  2. The difinition of alpha header, which consists of the mapping between every pixels and highlights and the ascii art warppered by [[ ]] every single line.

Require the generated lua file and set properly in your config of alpha-nvim (assign header to dashboard.section.header), it will be:

example2

If img2art generate too much lines, you can specify --quant n to reduce the color level of input image. n is a positive integer which should be smaller than 256.

img2art xxx.png --scale 0.1 --threshold 120 --save-raw ./xxx.lua --alpha --quant 16

Installation

requirements: typer[all], opencv-python, numpy

pip install img2art

Usage

img2art --help

result:

 Usage: img2art [OPTIONS] SOURCE

โ•ญโ”€ Arguments โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ *    source      TEXT  Path to image [default: None] [required]                                          โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
โ•ญโ”€ Options โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ --with-color    --no-with-color                                  Whether use color. If you specify       โ”‚
โ”‚                                                                  alpha, with-color will be forcely set   โ”‚
โ”‚                                                                  to True.                                โ”‚
โ”‚                                                                  [default: no-with-color]                โ”‚
โ”‚ --scale                            FLOAT                         Scale applied to image [default: 1.0]   โ”‚
โ”‚ --threshold                        INTEGER                       Threshold applied to image, default to  โ”‚
โ”‚                                                                  OSTU                                    โ”‚
โ”‚                                                                  [default: -1]                           โ”‚
โ”‚ --save-raw                         TEXT                          Whether to save the raw data            โ”‚
โ”‚                                                                  [default: None]                         โ”‚
โ”‚ --bg-color                         <INTEGER INTEGER INTEGER>...  Backgound color, (-1, -1, -1) for none  โ”‚
โ”‚                                                                  [default: -1, -1, -1]                   โ”‚
โ”‚ --fast          --no-fast                                        Whether use torch to accelerate when    โ”‚
โ”‚                                                                  you inputs have plenty of frames.       โ”‚
โ”‚                                                                  [default: no-fast]                      โ”‚
โ”‚ --chunk-size                       INTEGER                       Chunk size of Videos or Gifs when using โ”‚
โ”‚                                                                  torch.                                  โ”‚
โ”‚                                                                  [default: 1024]                         โ”‚
โ”‚ --alpha         --no-alpha                                       Whether generating lua code for         โ”‚
โ”‚                                                                  alpha-nvim.                             โ”‚
โ”‚                                                                  [default: no-alpha]                     โ”‚
โ”‚ --quant                            INTEGER                       Apply color quantization. [default: -1] โ”‚
โ”‚ --mapping                          TEXT                          User-define ascii characters, need to   โ”‚
โ”‚                                                                  be from light to dark. The quant will   โ”‚
โ”‚                                                                  be forcely set to length of mapping.    โ”‚
โ”‚ --loop          --no-loop                                        Loop the output when input is GIF or    โ”‚
โ”‚                                                                  Video, use Ctrl-C to end this.          โ”‚
โ”‚                                                                  [default: no-loop]                      โ”‚
โ”‚ --interval                         FLOAT                         Interval when playing GIF or Video      โ”‚
โ”‚                                                                  output.                                 โ”‚
โ”‚                                                                  [default: 0.05]                         โ”‚
โ”‚ --help                                                           Show this message and exit.             โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ
img2art path/to/image --scale 0.5 --with-color --threshold 127 --bg-color 255, 255, 255 --save-raw path/to/save.txt --alpha --quant 16

Use your own characters mappings:

img2art path/to/image --scale 0.1 --with-color --threshold 127 --bg-color 255, 255, 255 --save-raw path/to/save.txt --alpha --mapping " ^*!#@%"

For output of gif or video, you can write a shell script to play it or use some tools like rustyAscii.

Reference

bobibo