Metadata-Version: 2.4
Name: par-textual-image
Version: 0.1.0
Summary: Textual widget for full-color terminal image rendering (Kitty TGP, Sixel, iTerm2, half-block)
Project-URL: Homepage, https://github.com/paulrobello/par-textual-image
Project-URL: Documentation, https://github.com/paulrobello/par-textual-image/blob/main/README.md
Project-URL: Source, https://github.com/paulrobello/par-textual-image
Project-URL: Issues, https://github.com/paulrobello/par-textual-image/issues
Author-email: Paul Robello <probello@gmail.com>
Maintainer-email: Paul Robello <probello@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Paul Robello
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: halfblock,image,iterm2,kitty,sixel,terminal,textual,tui
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: End Users/Desktop
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows :: Windows 10
Classifier: Operating System :: Microsoft :: Windows :: Windows 11
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Multimedia :: Graphics
Classifier: Topic :: Terminals
Classifier: Typing :: Typed
Requires-Python: >=3.13
Requires-Dist: pillow>=11.0
Requires-Dist: rich>=14.0
Requires-Dist: textual>=3.0
Provides-Extra: dev
Requires-Dist: pyright>=1.1; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.25; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.11; extra == 'dev'
Description-Content-Type: text/markdown

# PAR Textual Image

## Table of Contents

* [About](#about)
* [Features](#features)
* [Supported Protocols](#supported-protocols)
* [Prerequisites](#prerequisites)
* [Installing](#installing)
* [Quick Start](#quick-start)
* [API Reference](#api-reference)
* [Environment Variables](#environment-variables)
* [Protocol Detection](#protocol-detection)
* [Installing for dev mode](#installing-for-dev-mode)
* [Contributing](#contributing)
* [License](#license)

[![PyPI](https://img.shields.io/pypi/v/par-textual-image)](https://pypi.org/project/par-textual-image/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/par-textual-image.svg)](https://pypi.org/project/par-textual-image/)
![Runs on Linux | MacOS | Windows](https://img.shields.io/badge/runs%20on-Linux%20%7C%20MacOS%20%7C%20Windows-blue)
![Arch x86-64 | ARM | AppleSilicon](https://img.shields.io/badge/arch-x86--64%20%7C%20ARM%20%7C%20AppleSilicon-blue)
![PyPI - License](https://img.shields.io/pypi/l/par-textual-image)

## About

PAR Textual Image is a Textual widget (`TerminalImage`) that renders full-color images in a terminal TUI by auto-detecting and using the best available terminal graphics protocol.
The library was built with [Textual](https://textual.textualize.io/), [Rich](https://github.com/Textualize/rich), and [Pillow](https://python-pillow.org/).
It runs on all major terminals that support Kitty TGP, Sixel, iTerm2 inline images, or falls back to half-block ANSI rendering.

## Features

- **Auto-detection** — probes for Kitty TGP, Sixel, and iTerm2 support at startup; falls back to half-block rendering
- **Manual override** — `force_protocol("kitty" | "sixel" | "iterm2" | "halfblock")`
- **Multiple input types** — file path, PIL `Image`, or `BytesIO`
- **Protocol-aware sizing** — uses terminal cell pixel dimensions for pixel-based protocols
- **Seamless Textual integration** — works as a drop-in widget with `compose()` / `mount()`
- **Type-safe** — fully typed codebase with strict pyright checking

## Supported Protocols

| Protocol | Method | Quality | Terminal Support |
|----------|--------|---------|------------------|
| Kitty TGP | Virtual placement | Full color | Kitty, KiTTY |
| Sixel | DCS escape sequences | Full color | xterm, mintty, Windows Terminal, others |
| iTerm2 | OSC 1337 inline | Full color | iTerm2 |
| Half-block | Unicode + ANSI colors | Medium | Any terminal with Unicode support |

## Prerequisites

* Python 3.13 or newer
* A terminal supporting at least one of the above protocols (half-block works everywhere)

## Installing

### Using pip

```bash
pip install par-textual-image
```

### Using uv

```bash
uv add par-textual-image
```

### Using pipx

```bash
pipx install par-textual-image
```

## Quick Start

```python
from textual.app import App, ComposeResult
from par_textual_image import TerminalImage

class ImageViewer(App):
    def compose(self) -> ComposeResult:
        yield TerminalImage("photo.png")

if __name__ == "__main__":
    ImageViewer().run()
```

### Force a specific protocol

```python
widget = TerminalImage()
widget.force_protocol("sixel")
widget.set_image("photo.png")
```

### Use a PIL Image directly

```python
from PIL import Image
img = Image.open("photo.png").resize((200, 100))
widget = TerminalImage(img)
```

## API Reference

### `TerminalImage`

The main Textual widget. Accepts an optional image source on construction.

| Method | Description |
|--------|-------------|
| `TerminalImage(source=None)` | Create widget with optional file path, `Image`, or `BytesIO` |
| `set_image(source)` | Set / replace the current image |
| `force_protocol(name)` | Switch protocol (`"kitty"`, `"sixel"`, `"iterm2"`, `"halfblock"`) |
| `protocol_info` | Property returning `(protocol_name, renderer_name)` |

### `detect_protocol()`

Auto-detect the best available protocol. Returns a string like `"kitty"`, `"sixel"`, `"iterm2"`, or `"halfblock"`.
Result is memoized — call once at startup.

### `query_cell_size()`

Query the terminal cell pixel dimensions. Returns `(width, height)`.

## Environment Variables

| Variable | Description |
|----------|-------------|
| `PAR_TERM_GRAPHICS` | Force a specific protocol (`kitty`, `sixel`, `iterm2`, `halfblock`) |
| `TEXTUAL_CELL_WIDTH` | Override detected cell width in pixels |
| `TEXTUAL_CELL_HEIGHT` | Override detected cell height in pixels |

## Protocol Detection

Detection priority:

1. `PAR_TERM_GRAPHICS` env var (if set and valid)
2. iTerm2 env vars (`TERM_PROGRAM=iTerm.app` or `LC_TERMINAL=iTerm2`)
3. Kitty TGP probe (queries the terminal, 100ms timeout)
4. Sixel probe (DA1 query checks for `4`)
5. Fallback: `halfblock`

## Installing for dev mode

Clone the repo and run the setup make target. Note `uv` is required.

```bash
git clone https://github.com/paulrobello/par-textual-image
cd par-textual-image
make setup
```

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, code style, commit conventions, and the pull request process.

## License

This project is licensed under the MIT License — see the [LICENSE](LICENSE) file for details.
