Metadata-Version: 2.4
Name: sketchkit
Version: 0.0.2
Summary: An universal toolkit for sketching
Requires-Python: >=3.12
Description-Content-Type: text/markdown
Requires-Dist: cairocffi>=1.7.1
Requires-Dist: cairosvg>=2.7.0
Requires-Dist: fastparquet>=2024.11.0
Requires-Dist: gdown>=5.2.0
Requires-Dist: matplotlib>=3.9.0
Requires-Dist: ndjson>=0.3.1
Requires-Dist: pandas>=2.3.2
Requires-Dist: patool>=4.0.1
Requires-Dist: pyunpack>=0.3
Requires-Dist: py7zr>=1.0.0
Requires-Dist: pyunpack>=0.3
Requires-Dist: rich>=14.1.0
Requires-Dist: svg-path>=7.0
Requires-Dist: svgpathtools>=1.7.1
Requires-Dist: scipy>=1.13.1
Requires-Dist: torch>=2.8.0
Requires-Dist: torchvision>=0.23.0
Requires-Dist: transformers>=4.44.2
Requires-Dist: wandb>=0.18.0
Requires-Dist: wget>=3.2
Requires-Dist: open-clip-torch<4,>=3.3.0
Requires-Dist: scikit-image>=0.25.2
Requires-Dist: ffmpeg>=1.4
Requires-Dist: svgwrite>=1.4.3
Requires-Dist: cssutils>=2.11.1
Requires-Dist: numba>=0.62.1
Requires-Dist: torch-tools>=0.1.5
Requires-Dist: visdom>=0.2.4
Requires-Dist: python-dotenv>=1.2.1
Requires-Dist: av>=16.0.1
Requires-Dist: hsluv>=5.0.4
Requires-Dist: moderngl>=5.12.0
Requires-Dist: trimesh>=4.10.1
Requires-Dist: diffusers>=0.36.0
Requires-Dist: opencv-contrib-python>=4.12.0.88
Requires-Dist: pydantic>=2.12.4
Requires-Dist: dashscope>=1.25.8
Requires-Dist: einops>=0.8.2
Requires-Dist: lmdb>=2.2.0
Requires-Dist: tensorboard>=2.20.0
Requires-Dist: accelerate>=1.13.0
Requires-Dist: rdp>=0.8

# SketchKit

SketchKit is a Python toolkit for loading, representing, processing, and rendering digital sketches. It is designed for researchers studying sketch data and developers building sketch-based applications, with unified APIs for datasets, vectorization, stroke ordering, image generation, colorization, animation, and 3D modeling.

[Documentation](https://cislab.hkust-gz.edu.cn/projects/sketchkit/docs/) · [Getting Started](docs/source/manual/0_get_started.md) · [API Reference](https://cislab.hkust-gz.edu.cn/projects/sketchkit/docs/api/sketchkit.html)

![SketchKit Examples](assets/examples.png)

## Why SketchKit?

- **Unified sketch representation** — work with different datasets through the same `Sketch`, `Path`, `Curve`, and `Point` hierarchy.
- **14 built-in datasets** — load popular sketch datasets on demand and filter samples through pandas metadata.
- **Composable sketch workflows** — connect compatible datasets, processing methods, and renderers through a shared representation.
- **2D and 3D rendering** — rasterize vector strokes, render expressive brush strokes, or project 3D sketches from multiple camera views.
- **Research methods behind simple interfaces** — use task-level APIs such as `Vectorizer`, `Orderer`, `Colorizer`, and `Modeler`.

## Installation

SketchKit requires Python 3.12 or later and uses [uv](https://docs.astral.sh/uv/) for dependency and environment management.

Install `uv` on macOS or Linux:

```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```

On Windows PowerShell:

```powershell
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
```

Clone or download this repository, then install the project environment:

```bash
git clone https://github.com/CISLab-HKUST/SketchKit.git
cd SketchKit
uv sync
```

### macOS distribution wheels

The macOS wheel includes Cairo and the native RTSC renderer. Installing a matching wheel does
not require Homebrew, a separate Cairo installation, or `DYLD_*` settings.
The usual Python dependencies are installed by pip. Choose a wheel matching
your Python version, Mac architecture, and macOS version.

### Building from source on macOS

The following native dependencies are needed on the **build machine**, including
for editable installs with `uv sync` or `pip install -e .`:

If `brew` is not available, install [Homebrew](https://brew.sh/) first and follow
the installer's instructions to add it to your shell environment. Then run:

```bash
brew install pkgconf glfw glew
```

Build a wheel for distribution:

```bash
DIFFVG_CUDA=0 uv build --wheel --python 3.12
```

Wheel builds on macOS, Linux, and Windows use `third_party/cairo/build.py` to compile
the pinned Cairo source and bundle it alongside Teddy, DiffVG, and RTSC in
`sketchkit/_native/`. The first build
downloads source archives. Subsequent builds reuse those archives and recompile.
`SKETCHKIT_CAIRO_SOURCE_CACHE` can point to a prepopulated source cache.
Linux Cairo builds also need `gperf` and `pkg-config` on the build machine.
Windows Cairo builds use the Visual Studio C/C++ toolchain.
Editable builds also prepare this private directory in the source tree. These generated
files are ignored by Git.
RTSC uses a hidden OpenGL window and requires a working graphical session.
`NPRRenderer` reports native rendering errors when used.

Verify the installation:

```bash
uv run python -c "import sketchkit; print('SketchKit is ready')"
```

### Troubleshooting: Cairo not found on macOS

This section applies to older wheels or source trees whose native runtime has
not been built. Current macOS wheels load their private Cairo automatically.

If importing SketchKit raises `OSError: no library called "cairo-2" was found`
or reports that `libcairo.2.dylib` cannot be loaded, first ensure that
`brew install cairo` has completed successfully. Some Python/Homebrew
configurations do not automatically search Homebrew's library directory.
Only if the error persists, provide that directory explicitly:

```bash
export DYLD_FALLBACK_LIBRARY_PATH="$(brew --prefix)/lib${DYLD_FALLBACK_LIBRARY_PATH:+:$DYLD_FALLBACK_LIBRARY_PATH}"
uv run python -c "import sketchkit; print('SketchKit is ready')"
```

The `export` applies to the current terminal and commands started from it.
To keep the setting for new zsh terminals, add the `export` line to `~/.zshrc`
and run `source ~/.zshrc` in the current terminal.

macOS can strip inherited `DYLD_*` variables when launching some uv builds.
If the `export` does not resolve the error, set the variable after uv starts:

```bash
uv run --no-sync env \
  DYLD_FALLBACK_LIBRARY_PATH="$(brew --prefix)/lib${DYLD_FALLBACK_LIBRARY_PATH:+:$DYLD_FALLBACK_LIBRARY_PATH}" \
  python -c "import sketchkit; print('SketchKit is ready')"
```

This fallback applies only to the command. To run a script with it,
replace `-c "import sketchkit; print('SketchKit is ready')"` with its filename.
Re-running `uv sync` alone will not install Cairo or configure its search path.

## Quick Start

Load a dataset, retrieve a vector sketch, and render it to a PNG:

```python
from sketchkit.datasets import OpenSketch
from sketchkit.renderer import CairoRenderer

dataset = OpenSketch()
sketch = dataset[0]

renderer = CairoRenderer()
image = renderer.render(sketch)
image.save("rendering.png")
```

Save the example as `quickstart.py`, then run:

```bash
uv run python quickstart.py
```

The script creates `rendering.png`. On first use, the OpenSketch adapter downloads its dataset automatically. Datasets are cached under `~/.sketchkit/datasets` by default; pass `root=...` to choose another location, or `cislab_source=True` to use the CIS LAB mirror where supported.

For complete pipelines covering vectorization, animation, NPR rendering, and stylization, see the [Getting Started guide](docs/source/manual/0_get_started.md).

## Unified Sketch Representation

Built-in dataset adapters convert sketches to the same vector structure:

```text
Sketch
└── Path
    └── Curve
        ├── Start Point
        ├── Control Point 1
        ├── Control Point 2
        └── End Point
```

A `Sketch` contains paths, each `Path` represents a continuous stroke, and each `Curve` is a cubic Bézier segment. Points store coordinates and can also carry pressure, thickness, color, opacity, and custom attributes.

Learn more in the [representation guide](docs/source/manual/1_representation.md).

## Supported Datasets

SketchKit currently includes 14 dataset adapters:

- LineDrawer
- QuickDraw
- ControlSketch
- TU-Berlin
- Tracing vs. Freehand
- OpenSketch
- SketchX-PRIS
- Sketchy
- PhotoSketching
- GMU Sketch Cleanup
- FS-COCO
- DifferSketching
- CreativeSketch
- SketchIME

Download behavior, source availability, metadata, and original representation vary by dataset. Check the [dataset guide](docs/source/manual/2_datasets.rst) and its dataset-specific pages before starting a large download.

All dataset classes expose `items_metadata`, a pandas `DataFrame` for filtering by fields such as category, split, and identifier:

```python
from sketchkit.datasets import ControlSketch

dataset = ControlSketch()
bears = dataset.items_metadata[
    (dataset.items_metadata["category"] == "bear")
    & (dataset.items_metadata["split"] == "validation")
]
sketches = [dataset[row.id] for _, row in bears.head(100).iterrows()]
```

See the [dataset guide](docs/source/manual/2_datasets.rst) for storage, lazy loading, metadata, and dataset-specific documentation.

## Methods

SketchKit organizes methods around sketches as the central representation. The table lists implementations documented in this repository; availability and setup requirements vary by implementation.

| Task | Documented implementations |
| --- | --- |
| Cleanup | MasterSketch |
| Vectorization | DeepVecSIG24, LineDrawer |
| Stroke ordering | Fu, LineDrawer |
| Animation | RIFE |
| Stylization | NeuralBrushstroke |
| Image to sketch | HED, PhotoSketch, SwiftSketch, AniLines |
| Sketch to image | ControlNet, Pix2Pix, T2I-Adapter, Nano Banana 2 |
| Colorization | ControlNet Lineart, ControlNet Scribble, MangaNinja |
| 3D modeling | Teddy, SENS, Seed3D |
| Non-photorealistic rendering | Suggestive Contours, Ridges and Valleys, Apparent Ridges |

Some methods require pretrained weights, a CUDA-capable GPU, external services or credentials, or additional assets. Check the corresponding page in the [methods guide](docs/source/manual/4_methods.rst) before use.

## Rendering

The rendering modules cover both 2D and 3D sketch visualization:

- **CairoRenderer** rasterizes vector sketches with configurable canvas and stroke options.
- **CialloRenderer** provides GPU-accelerated, brush-based vector stroke rendering.
- **DiffVGRenderer** projects `Sketch3D` Bézier curves into one or more camera views.

See the [2D renderer guide](docs/source/manual/renderers/renderer.md) and [3D renderer guide](docs/source/manual/renderers/3Drenderer.md) for examples and render options.

## Documentation

The complete documentation is available at:

<https://cislab.hkust-gz.edu.cn/projects/sketchkit/docs/>

Useful entry points:

- [Getting Started](docs/source/manual/0_get_started.md)
- [Sketch Representation](docs/source/manual/1_representation.md)
- [Supported Datasets](docs/source/manual/2_datasets.rst)
- [Methods](docs/source/manual/4_methods.rst)
- [Rendering](docs/source/manual/6_renderers.rst)
- [API Reference](https://cislab.hkust-gz.edu.cn/projects/sketchkit/docs/api/sketchkit.html)

## Contributing

Contributions for new datasets and processing methods are welcome. The manuals describe the expected unified interfaces, metadata conventions, module layout, tests, and GitHub workflow:

- [Dataset Contribution Guide](docs/source/manual/3_dataset_contribution.md)
- [Method Contribution Guide](docs/source/manual/5_method_contribution.md)
