Skip to main content

tchipng

PNG with human-visible color compression by TchiTchi.

tchipng 把行优先 RGB 收成最多 256 个原图里真实出现过的颜色,再写成索引 PNG。输出是 JPEG 时,先做同样的量化,再按质量编码。命令行和 Python 调用的都是同一份 C 核心。

快速上手

发布到 PyPI 之后:

pip install tchipng

当前仓库内:

pip install .
import tchipng

tchipng.compress("input.png", "output.png")
tchipng.compress("input.png", "output.jpg", quality=90)

许可证是 Apache License 2.0。third_party/ 里的 stb 头文件是 public domain,只给命令行读写 PNG 和 JPEG。

布局

路径 内容
tchipng.h、tchipng.c 压缩与量化
tchipng_cli.c 命令行
python/tchipng/ import tchipng 和 tchipng 命令入口
android/tchipng_jni.c Android JNI 绑定,默认 CMake 不编译它
third_party/ stb 图像读写

build/、build-python/、build-android/、build-ios/、build-harmony/ 是本地编译结果,已写入 .gitignore。

命令行

依赖 C 编译器、CMake 3.16 或更高版本,以及 zlib。

cmake -S . -B build
cmake --build build
./build/tchipng input.png output.png
./build/tchipng -q 90 input.png output.jpg

用法是 tchipng [-q quality] <input> <output>。

  • 输入由 stb 解码,按 RGB 读取 PNG 或 JPEG。
  • 输出扩展名是 .png 时,写出索引 PNG。
  • 输出扩展名是 .jpg 或 .jpeg 时,量化后写 JPEG。-q 范围是 1 到 100,省略时为 90。

本机产物:

  • build/tchipng
  • build/libtchipng.dylib(Linux 上是 libtchipng.so)
  • build/libtchipng.a

Python

需要 Python 3.10 或更高版本。通过 pip 安装时还需要 CMake 3.31 或更高版本,因为安装步骤会编译并装入动态库和命令行。

在仓库根目录执行:

pip install -e .

之后可以直接调用已安装的命令,效果与上面的 C 程序相同:

tchipng input.png output.png
tchipng -q 90 input.png output.jpg

compress 按输出扩展名选择格式,和命令行相同。已经持有行优先 RGB 时,stride 是每行字节数,紧凑排列时为 width * 3。

from tchipng import compress_rgb, quantize_rgb

png = compress_rgb(rgb, width, height, width * 3)
quantized = quantize_rgb(rgb, width, height, width * 3)

compress_rgb 返回完整 PNG 字节。quantize_rgb 返回量化后的紧凑 RGB,长度是 width * height * 3。参数不合法、内存不足或编码失败时抛出 TchiPngError。

改了 C 源码之后,需要再执行一次 pip install -e .。编辑安装不会在每次运行时自动重编译。

C 接口

int tchipng_compress_rgb(const uint8_t *rgb, int width, int height, int stride,
                         uint8_t **out_png, size_t *out_size);
int tchipng_quantize_rgb(const uint8_t *rgb, int width, int height, int stride,
                         uint8_t **out_rgb, size_t *out_size);
void tchipng_free(void *ptr);

成功返回 0。返回的缓冲区用 tchipng_free 释放。

Metadata

Release files for tchipng 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tchipng 0.1.0
File Size Uploaded Provenance Actions
tchipng-0.1.0.tar.gz 112.7 kB Without provenance Details for tchipng-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tchipng 0.1.0
File Interpreter ABI Platform Actions
tchipng-0.1.0-cp312-cp312-macosx_26_0_arm64.whl CPython 3.12 CPython 3.12 macOS 26.0+ ARM64 Details for tchipng-0.1.0-cp312-cp312-macosx_26_0_arm64.whl

Total release size: 201.8 kB

Release files / tchipng-0.1.0.tar.gz

Download URL tchipng-0.1.0.tar.gz
Size 112.7 kB
Tags Source
SHA-256 checksum
How to use checksums
64df0c66724e33a25eacefd25406e3f71172960c383b5a8826eb751d8d87ad82
BLAKE2b-256 checksum
How to use checksums
0211966cc63cde58959709e535db7bf75324c44355e98461f4496ad01dfe8103
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / tchipng-0.1.0-cp312-cp312-macosx_26_0_arm64.whl

Download URL tchipng-0.1.0-cp312-cp312-macosx_26_0_arm64.whl
Size 89.1 kB
Tags CPython 3.12 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
eac69cc2850a84f483dd8e29917d0d7d4a03761ffd850eeef83f7fed82ffe140
BLAKE2b-256 checksum
How to use checksums
a7886cfe892ada1d1cc9751da8b1b9ea10e0998afae70316f72d969d9fa8a0ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Security for tchipng 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page