Skip to main content

ANSI art image processing and colored terminal text

Project description

image

image image image image

Chromatic is a library for processing and transforming ANSI escape sequences (colored terminal text).

It offers a collection of algorithms and types for a variety of use cases:

  • Image-to-ASCII / Image-to-ANSI conversions.
  • ANSI art rendering, with support for user-defined fonts.
  • A ColorStr type which enables precise low-level control over ANSI-escaped strings through a convenient interface.
  • colorama-style wrappers (Fore, Back, Style).
  • Parametrization of ANSI color bit formats, allowing arbitrary conversion between 16-color, 256-color, and true-color (RGB) colorspace on any object implementing a colorbytes buffer.
  • Et Cetera 😲

Usage

ColorStr

from chromatic import ColorStr

base_str = 'hello world'

red_fg = ColorStr(base_str, 0xFF0000)

assert red_fg.base_str == base_str
assert red_fg.rgb_dict == {'fg': (0xFF, 0, 0)}
assert red_fg.ansi == b'\x1b[38;5;196m'

ColorStr can handle different signatures for color_spec:

from chromatic import ColorStr

assert all(
	ColorStr('*', cs) == ColorStr('*', 0xFF0000)
    for cs in [b'\x1b[38;5;196m', b'\xff\x00\x00', (0xFF, 0, 0), {'fg': 0xFF0000}]
)

The ANSI color format can be given as an argument, or returned by ColorStr.as_ansi_type() as a new instance.

from chromatic import ColorStr, ansicolor24Bit, ansicolor4Bit

truecolor = ColorStr('*', 0xFF0000, ansi_type=ansicolor24Bit)
a_16color = truecolor.as_ansi_type(ansicolor4Bit)

# each ansi color format has an alias that can be used in place of the type object
assert a_16color == truecolor.as_ansi_type('4b')

assert truecolor.ansi_format is ansicolor24Bit and truecolor.ansi == b'\x1b[38;2;255;0;0m'
assert a_16color.ansi_format is ansicolor4Bit and a_16color.ansi == b'\x1b[31m'

Adding and removing specific ANSI codes from the escape sequence:

import chromatic as cm

boring_str = cm.ColorStr('hello world')

assert boring_str.ansi == b''

bold_str = boring_str + cm.SgrParameter.BOLD
assert bold_str.ansi == b'\x1b[1m'

# use ColorStr.update_sgr() to remove and add SGR values
unbold_str = bold_str.update_sgr(cm.SgrParameter.BOLD)
assert unbold_str == boring_str
assert bold_str == unbold_str.update_sgr(cm.SgrParameter.BOLD)

Image-to-ANSI conversion

Converting an image into an array of ANSI-escaped characters, then rendering the ANSI array as another image:

from chromatic.color import ansicolor4Bit
from chromatic.ascii import ansi2img, img2ansi
from chromatic.data import UserFont, butterfly

input_img = butterfly()

font = UserFont.IBM_VGA_437_8X16

# by default, `char_set` would be sorted based on the relative weight of glyphs in the font
# but because `sort_glyphs` is set to False, `char_set` will be directly mapped to the image brightness
#            | <- index 0 is the 'darkest'
char_set = r"'·,•-_→+<>ⁿ*%⌂7√Iï∞πbz£9yîU{}1αHSw♥æ?GX╕╒éà⌡MF╝╩ΘûǃQ½☻Ŷ┤▄╪║▒█"
#                                            index -1 is the 'brightest' -> |

ansi_array = img2ansi(input_img, font, sort_glyphs=False, char_set=char_set, ansi_type=ansicolor4Bit, factor=200)

# ansi2img() returns a PIL.Image.Image object
ansi_img = ansi2img(ansi_array, font, font_size=16)
ansi_img.show()

Installation

Install the package using pip:

pip install chromatic-python

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

chromatic_python-0.3.4.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

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

chromatic_python-0.3.4-py3-none-any.whl (923.5 kB view details)

Uploaded Python 3

File details

Details for the file chromatic_python-0.3.4.tar.gz.

File metadata

  • Download URL: chromatic_python-0.3.4.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for chromatic_python-0.3.4.tar.gz
Algorithm Hash digest
SHA256 f1cb56dee802685c61205447692a5cfa320adbdd4e00c32f2395dd2f87d9cf88
MD5 7de1794bd2031fff5c8a7720e3a69729
BLAKE2b-256 3247745e27ffea19f557f9c693f8a64ddbb65fe49994eab0f16c14fbbf9e0740

See more details on using hashes here.

File details

Details for the file chromatic_python-0.3.4-py3-none-any.whl.

File metadata

File hashes

Hashes for chromatic_python-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d589fcb6dc1ffee22491e3af409d695e7238b459653dcc4d3dedafe5cf79a8
MD5 06db0e5b917d906409052fbaa1fa2765
BLAKE2b-256 e9dc282f14142ccc4af9ddd0378365cdc9bf1b600d5444da542520a51ea11a1e

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