Skip to main content

No project description provided

Project description

Cover photo

This package allows you to turn images (only .jpg and .png tested) into ASCII art drawings.
Inspired by ascii-view.

Documentation

Full documentation is available at: asciify-them.readthedocs.io

Features

  • CLI: This software can be accessed both as a Python library and as a CLI;
  • Colored output: ANSI color codes allow to print colors (requires a modern terminal);
  • Resizing flexibility: Images are scaled to keep aspect ratio and fit the image to the terminal, but both options can be disabled;
  • Edge detection: Sobel and Canny algorithm are used to highlight edges;
  • Output flexibility: Resulting images can be saved in a file (both using the terminal to determine optimal size or providing custom height and/or width).

Installation

The package can be installed through PyPi:

pip install asciify-them

But also from source:

git clone https://github.com/ndrscalia/asciify-them
cd <repo-dir>
python -m venv .venv
source .venv/bin/activate
pip install -e .

Usage

CLI

The only required argument is the path to the image:

asciify <path/to/image> [OPTIONS]

The following options are available:

  • -bw, --black_white: Set the output to black&white.
  • -e, --edges: Enable edge detection.
  • -w, --width: Provide custom width. If not specified, terminal's size is going to determine this value. This value can be specified only when f_type='wide'.
  • -he, --height: Provide custom height. If not specified, terminal's size is going to determine this value. This value can be specified only when f_type='tall'.
  • -ar, --no_aspect_ratio: Disable original aspect ratio's protection.
  • -f, --factor_type: Choose the downsampling factor type among the following values: in_terminal, wide, tall.
  • -b, --blur: Provide a list with kernel size as a tuple, std for x axis, std for y axis. For more details refere to the docs for cv2.GaussianBlur. Changing the dafault values allow to tweak edge detection.
  • ct, --canny_threshold: Provide edges detection threshold as a tuple. For more details refer to the docs for cv2.Canny.
  • -at, --angles_threshold: Provide kernel size for angles calculation as an integer.
  • -o, --output: Provide the output's path. If not specified, uses stdout (e.g.: terminal).

Details

The different factors available are meant for different scenarios:

  • in_terminal allows to keep the output inside the terminal keeping aspect ratio;
  • wide is better suited for images which are wider than taller but the output does not stay in the terminal. This option is also optimal for conversion to .png through ansee, regardless of the relation between height and width;
  • tall is better suited for images which are taller than wider but the output does not stay in the terminal; If aspect ratio's protection is disabled, output will be squished by a factor to stay in the terminal.

Python library

This package can also be used as a python library. Most of the API is exposed to the user, but a convenient wrapper is also available for simpler use cases.

from asciify import asciify

# Minimal use
result = asciify("path/to/image")
print(result)

# More advanced use
result = asciify(
    "path/to/image",
    color_mode="bw",
    edges_detection=True,
    f_type="tall"
)

with open("output.txt", "w") as f:
    f.write(result)

The .txt output can be used with ansee to get a .png file out of it.
If needed, the core classes can be used as follows:

processor = ImgProcessor(image_path)

if not height and not width:
    term_height, term_width = processor.calculate_print_size()
else:
    term_height, term_width = height, width

ds_f = processor.calculate_downsample_factor(
    term_height=term_height,
    term_width=term_width,
    keep_aspect_ratio=keep_aspect_ratio,
    f_type=f_type
)

ds_img = processor.downsample_image(
    f=ds_f,
    keep_aspect_ratio=keep_aspect_ratio
)

img_hsv = processor.convert_to_hsv(image=ds_img)

angles = processor.calculate_angles(
    image=ds_img,
    k_size=angles_thresh
)

edges = processor.detect_edges(
    image=ds_img,
    blur=blur,
    canny_thresh=canny_thresh
)

renderer = Renderer(
    color_mode=color_mode,
    charset=DEFAULT_CHARSET
)

if edges_detection:
    return renderer.draw_in_ascii_with_edges(img_hsv=img_hsv, angles=angles, edges=edges)
else:
    return renderer.draw_in_ascii(img_hsv=img_hsv)

Examples

Example photo

Testing

To test the codebase check tests/README.md.

Future updates and possible contributions

  • Allow custom charset with different number of characters;
  • Allow tuning brightness for better piping to ansee;
  • Improve edges' detection.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asciify_them-1.0.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asciify_them-1.0.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file asciify_them-1.0.0.tar.gz.

File metadata

  • Download URL: asciify_them-1.0.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for asciify_them-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f4502d26e76730f855fdef4227037f7c0fda5d3e6bb4811daf032a54bcf974f8
MD5 44351e94ea634f864d0553df52e6c894
BLAKE2b-256 b1a6311bc60d68973765ad77a167718249d1ec5d53e0e94cb86c7d568b8bc40a

See more details on using hashes here.

File details

Details for the file asciify_them-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: asciify_them-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for asciify_them-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25a4a4c88deb8ae9105f8235243f17fbb72fe2af349a9aa1fe0b01b9ccd409d8
MD5 0f472429ca3ae37a3bdbd45fe5783ddd
BLAKE2b-256 83fc4a851ccc4fdab77b4b1f262cd35c917025c19a57a54740b5266ec025e199

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page