Skip to main content

A Python library for enhanced terminal display.

Project description

🐱‍👤 cobra-color 🐱‍👤

PyPI version Python codecov License

Tests Github stars

About

A lightweight and easy-to-use Python library for enhanced terminal display: simple text color/style conventions and image-to-terminal rendering.

  • Python: 3.9+
  • Runtime deps: Pillow (>=9,<11), NumPy (>=1.21,<2)

Features

  • 🚀 High compatibility with Python’s built-in str type.
  • 🚀 Easy and intuitive creation of color strings.
  • 🚀 Convert images to colored terminal output in a single step.

Installation

Stable (once published):

pip install cobra-color

Quick Start

  • Render a text in the terminal:

    from cobra_color import cstr, safe_print
    
    c_text_1 = cstr("Hello World!", fg="r", styles=["bold"])
    # Print directly from the terminal
    print(c_text_1)
    
    c_text_2 = cstr("Hello World!", fg=(255, 255, 255), styles=["udl", "bold"])
    # Alternatively, you can use safe_print() to automatically support progress bar modes like tqdm and rich.
    safe_print(c_text_2)
    
    # Merge `c_text_1` and `c_text_2` while preserving their colors and style formatting.
    c_text_3 = c_text_1 + c_text_2
    
    # You can continue to use str's proprietary functions and keep the existing colors and styles.
    c_text_1.upper()
    
  • Render an image in the terminal:

    from cobra_color.render import imgfile_to_ansi, safe_print
    
    # ASCII art
    safe_print(imgfile_to_ansi("example.jpg", width=80, mode="ascii"))
    
    # Half-block color (recommended for truecolor terminals)
    imgfile_to_ansi("example.jpg", width=80, mode="half-color", display=True)
    
  • Render some text with fonts in the terminal:

    from cobra_color.draw import fonttext_to_ansi, FontName, safe_print
    
    # Borderless grayscale font
    safe_print(fonttext_to_ansi(
        "Hello World!",
        font=FontName.LLDISCO,
        mode="half-gray",
        trim_border=True
    ))
    

Image Modes

  • ascii: monochrome ASCII using a density charset.
  • color: colorized character fill.
  • half-color: half-block characters with color (higher density, good visual quality).
  • gray: grayscale characters.
  • half-gray: half-block grayscale.

Tip: For best results, use a TrueColor-capable terminal and a monospaced font.

Requirements

  • Python >= 3.9
  • Pillow >= 9.0, < 11
  • NumPy >= 1.21, < 2.0

License

See LICENSE in the repository.

Links

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

cobra_color-1.3.5.tar.gz (29.6 kB view details)

Uploaded Source

Built Distribution

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

cobra_color-1.3.5-py3-none-any.whl (31.1 kB view details)

Uploaded Python 3

File details

Details for the file cobra_color-1.3.5.tar.gz.

File metadata

  • Download URL: cobra_color-1.3.5.tar.gz
  • Upload date:
  • Size: 29.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for cobra_color-1.3.5.tar.gz
Algorithm Hash digest
SHA256 13a283ab0b1660f6f7b12e4d755532df73dee2e4dd022f39f309b0094108eb1e
MD5 05fda540bfe6a7b98cbd94aeeca5818c
BLAKE2b-256 89e85d4bbb6aa2d244e4a12880daa2707b6866f30ccc7315bca868a867e1e6b7

See more details on using hashes here.

File details

Details for the file cobra_color-1.3.5-py3-none-any.whl.

File metadata

  • Download URL: cobra_color-1.3.5-py3-none-any.whl
  • Upload date:
  • Size: 31.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for cobra_color-1.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7ad75f8cc893f58592c7a05e2e72d348aa497ddf6e7e3c9ab78d6729057eecab
MD5 de110aa07df6e5a95e26fceca466f2ba
BLAKE2b-256 01ca20521b47873029b64905234addd786302b9ffcd04fc08aaa2dea34332c4e

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