Skip to main content

tchipng

PNG with human-visible color compression by TchiTchi.

tchipng 把行优先 RGB 收成最多 256 个原图里真实出现过的颜色,再写成索引 PNG。输出是 JPEG 时,先做同样的量化,再按质量编码。

快速上手

pip3 install tchipng
import tchipng

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

命令行同样可用:

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

输入是 PNG 或 JPEG。输出扩展名是 .png 时写索引 PNG;是 .jpg 或 .jpeg 时写 JPEG。-q 和 quality 的范围是 1 到 100,省略时为 90。

原图 768×1280,约 1.5MB(1,455,017 字节)。tchipng 索引 PNG 约 443KB(442,532 字节)。下面两张图缩到 180px 宽,只作对照。

原图 tchipng
原图 tchipng

Python

已经持有行优先 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 接口

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 释放。

从源码编译命令行需要 C 编译器、CMake 3.16 或更高版本,以及 zlib:

cmake -S . -B build
cmake --build build
./build/tchipng input.png output.png

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

Metadata

Release files for tchipng 0.2.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.2.0
File Size Uploaded Provenance Actions
tchipng-0.2.0.tar.gz 329.5 kB Without provenance Details for tchipng-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 422.0 kB

Release files / tchipng-0.2.0.tar.gz

Download URL tchipng-0.2.0.tar.gz
Size 329.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0fbe74146ff20959dd06a39c0eddba8eb962e5cfda829a19ff2996df01061d00
BLAKE2b-256 checksum
How to use checksums
25388a983a09bfc9888a06687b4c5271f1299fa8f7c2d253f01ae4178be0e356
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.2.0-cp312-cp312-macosx_26_0_arm64.whl

Download URL tchipng-0.2.0-cp312-cp312-macosx_26_0_arm64.whl
Size 92.6 kB
Tags CPython 3.12 macOS 26.0+ ARM64
SHA-256 checksum
How to use checksums
f96fc564f69b270a640d61a08fffc23319e258ae165e88975b1c1ad4dd10d7c7
BLAKE2b-256 checksum
How to use checksums
2aa7f62b60c84f52633f32113d7413c0b241228e64de707b3293d464c2060e47
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.2.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.2.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