image-to-ascii

repository·master·Indexed 23 days ago

https://github.com/ionicabizau/image-to-ascii

A Node.js module (version 3.3.0) that converts images from local paths, URLs, or Buffers into ASCII art. It supports colored ANSI output, custom character sets, and fine-grained control over size, aspect ratio, and pixel mapping. The library recommends installing GraphicsMagick as a system dependency and provides an optional CLI via image-to-ascii-cli.

Tokens
2.4K
Snippets
6
Records
13
Agent score
80%

What's inside image-to-ascii

  1. Install dependencies for webcam example

    master

    The webcam.sh script in the example folder requires streamer to capture webcam pictures.

    On Ubuntu:

    sudo apt-get install streamer

    On CentOS / RHEL:

    sudo yum install --enablerepo epel GraphicsMagick

    To run the webcam script:

    sh webcam.sh
    sh webcam.sh
  2. Install GraphicsMagick dependency

    master

    The image-to-ascii library recommends installing GraphicsMagick on your system. If GraphicsMagick is not found, the library will attempt to install lwip by compiling C/C++ components automatically.

    Install GraphicsMagick using your operating system's package manager:

    # Ubuntu
    $ sudo apt-get install graphicsmagick
    
    # Fedora
    $ sudo dnf install GraphicsMagick
    
    # CentOS / RHEL
    $ sudo yum install --enablerepo epel GraphicsMagick
    
    # OS X
    $ brew install graphicsmagick
    
    # Windows (via Chocolatey)
    # Note: Restart cmd/PowerShell after installation
    $ choco install graphicsmagick
  3. Install image-to-ascii

    master

    You can install the image-to-ascii Node.js module using npm or yarn.

    # Using npm
    npm install --save image-to-ascii
    
    # Using yarn
    yarn add image-to-ascii

    If you prefer a command-line interface, you can install the CLI version globally:

    npm install --global image-to-ascii-cli
    # or
    yarn global add image-to-ascii-cli
    npm install --save image-to-ascii
  4. Configure pixel and matrix asciifier options

    master

    Customize how pixels are converted to characters and how the final string is constructed using these options:

    Matrix asciifier options

    • stringify (Boolean): If false, pixel objects will not be stringified.
    • concat (Boolean): If false, pixel objects will not be joined together.

    Pixel asciifier options

    • pixels (Array|String): The characters used for conversion (default: " .,:;i1tfLCG08@").
    • reverse (Boolean): If true, creates a negative image effect (default: false).
    • colored (Boolean): If true, output includes ANSI styles (default: true).
    • bg (Boolean): If true, uses the background color for coloring (default: false).
    • fg (Boolean): If true, uses the foreground color for coloring (default: true).
    • white_bg (Boolean): Enables a white background for transparent pixels (default: true).
    • px_background (Object): Custom background color using r, g, and b values.
  5. Use the imageToAscii function

    master

    The imageToAscii(source, options, callback) function converts an image into ASCII art.

    Parameters

    • source (String|Buffer): The path or URL to the image, or a Buffer object.
    • options (Object|String): A path to an image file or an options object to configure the conversion process.
    • callback (Function): The callback function executed after the conversion. It receives the resulting ASCII art.
    imageToAscii(source, options, callback)
  6. Configure size and aspect ratio in imageToAscii

    master

    You can control the dimensions and aspect ratio of the resulting ASCII art using the following options fields:

    • pxWidth (Number): The pixel width used for aspect ratio calculation (default: 2).
    • size (Object): Defines the output size. It is interpreted by compute-size:
      • height (Number|String): The height value (default: "100%").
      • width (Number|String): The width value (default: computed value to keep aspect ratio). Optional if height is provided.
    • size_options (Object): Advanced options for compute-size:
      • screen_size (Object): Defines the terminal/screen dimensions (width and height). Defaults to terminal size.
      • px_size (Object): Defines pixel dimensions (width and height). Defaults to 1 for both.
      • preserve_aspect_ratio (Boolean): If false, aspect ratio is ignored (default: true).
      • fit_screen (Boolean): If false, the result will not be constrained to fit the screen (default: true).
  7. Use imageToAscii() to convert images

    master

    The imageToAscii(source, options, callback) function converts an image (from a local path, URL, or Buffer) into ASCII art.

    Basic usage:

    const imageToAscii = require("image-to-ascii");
    
    imageToAscii("https://example.com/image.png", (err, converted) => {
        console.log(err || converted);
    });

    Usage with options (e.g., disabling color):

    imageToAscii("https://example.com/image.jpg", {
        colored: false
    }, (err, converted) => {
        console.log(err || converted);
    });
    const imageToAscii = require("image-to-ascii");
    
    imageToAscii("https://octodex.github.com/images/octofez.png", (err, converted) => {
        console.log(err || converted);
    });
  8. Configure pixel and color options in imageToAscii

    master

    Control how pixels are mapped to characters and how colors are applied to the output.

    Pixel Asciifier Options

    • pixels (Array|String): Characters used for converting pixels (default: " .,:;i1tfLCG08@").
    • reverse (Boolean): If true, creates a negative image effect (default: false).
    • colored (Boolean): If true, output contains ANSI styles (default: true).
    • bg (Boolean): If true, the background color is used for coloring (default: false).
    • fg (Boolean): If true, the foreground color is used for coloring (default: true).
    • white_bg (Boolean): Turns on white background for transparent pixels (default: true).
    • px_background (Object): Custom background color using { r, g, b } values.
  9. Configure imageToAscii sizing options

    master

    You can control the dimensions of the resulting ASCII art using the size and size_options properties.

    Size Options

    • size (Object): Interpreted by compute-size.
      • height (Number|String): The height value (default: "100%").
      • width (Number|String): The width value (default: computed value to keep aspect ratio). Optional if height is provided.

    Size Configuration (size_options)

    • screen_size (Object): Defaults to terminal width and height.
      • width (Number): The screen width.
      • height (Number): The screen height.
    • px_size (Object): The pixel size.
      • width (Number): Default 1.
      • height (Number): Default 1.
    • preserve_aspect_ratio (Boolean): If false, the aspect ratio will not be preserved (default: true).
    • fit_screen (Boolean): If false, the result size will not fit to screen (default: true).
  10. Configure matrix and stringifier options in imageToAscii

    master

    Control how the pixel matrix is processed into the final string output.

    Matrix Options

    • stringify (Boolean): If false, pixel objects will not be stringified (default: true).
    • concat (Boolean): If false, pixel objects will not be joined together (default: true).

    Stringifier Customization

    • stringify_fn (Function): A custom function that receives the pixels matrix and the options object. Use this to implement your own stringification logic.
    • image_type (String): Explicitly provide the image type.