ansimage
ansimage is a versatile Rust library and command-line tool for converting images into colorful terminal ANSI art.
It offers a high degree of control over character sets, color palettes, and output size, using perceptually uniform color calculations to generate high-quality results.
Features
- Multiple Character Modes: Render images using standard ASCII brightness ramps, high-fidelity Unicode block characters, or your own custom character sets.
- Advanced Color Handling: Supports 24-bit "truecolor" output as well as color quantization for terminals with limited palettes (e.g., 256 or 16 colors).
- High-Quality Processing: Uses the L*u*v* color space for perceptually accurate color comparisons and
imagequantfor high-quality dithering and palette mapping. - Performance: Image processing is parallelized using Rayon to take advantage of multiple CPU cores.
- Flexible Sizing: Easily fit the output to specific dimensions while preserving aspect ratio, or scale to an exact character width and height.
- Simple CLI and Library API: Use it as a quick command-line tool or integrate it directly into your Rust projects.
Installation
As a Command-Line Tool
Ensure you have the Rust toolchain installed. You can then install ansimage directly from Crates.io:
As a Library
Add ansimage as a dependency in your Cargo.toml file:
[]
= "0.1.0" # Replace with the latest version
Usage
Command-Line Interface
The CLI provides a straightforward way to convert an image. The only required argument is the input file path.
Basic Conversion
This command will process photo.jpg and print the resulting ANSI art to your terminal.
Saving to a File
Use the --output or -o flag to save the result to a text file. You can combine this with --quiet to suppress terminal output.
For a full list of commands, run:
Library API
Integrating ansimage into your own project is simple. The main entry point is the convert function, which takes an image path and a Settings struct.
Here's a basic example:
use ;
use Path;
Configuration
You can customize the output by modifying the Settings struct.
size: Control the outputwidth,height, andSizeMode(Fitvs.Exact).characters: Choose aCharacterMode(Ascii,Unicode,Custom),ColorMode(OneColorvs.TwoColor), and adjust the font'saspect_ratio.colors: Enable or disableis_truecolormode. Whenfalse, you must provide apaletteofimage::Rgb<u8>colors.advanced: Configure theresize_filterand enable/disabledithering.
Example: Custom Unicode Settings
use ;
let custom_settings = Settings ;
// Use this custom_settings object with the `convert` function.
License
This project is licensed under the MIT License. See the LICENSE file for details.