Skip to main content

High-level KTX2 <-> image (png/jpg/webp) conversion powered by Basis Universal (basisu).

Project description

alktx2

alktx2 是一个高集成度的图片 ↔ KTX2 转换库:用户 pip install 后即可通过少量函数完成:

  • png/jpg/webp/tga/tiff → ktx2(选择 Basis Universal 的编码算法,并暴露关键调参)
  • ktx2 → png/jpg/webp/tga/tiff(把 KTX2 解码为 RGBA8,再编码成常见图片格式)

项目目标是:接口极简、参数可控、性能足够好、并尽量保留 basisu 的能力(在 Python 侧以更友好的方式暴露)

支持的图片格式(与 KTX2 的互转)

方向 格式 扩展名 / MIME 要点 实现说明
图片 → KTX2 PNG .png / image/png stb_image 解码为 RGBA8
图片 → KTX2 JPEG .jpg .jpeg / image/jpeg stb_image
图片 → KTX2 WebP .webp / image/webp libwebp
图片 → KTX2 TGA .tga / image/x-tga stb_image
图片 → KTX2 TIFF .tif .tiff / image/tiff libtiff(常见压缩与 RGBA 接口可读;极冷门 photometric 可能失败)
KTX2 → 图片 PNG format="png" stb_image_write,未压缩
KTX2 → 图片 JPEG format="jpg" stb_image_write
KTX2 → 图片 WebP format="webp" libwebp
KTX2 → 图片 TGA format="tga" stb_image_write未使用 RLE 的 TGA
KTX2 → 图片 TIFF format="tif"format="tiff" libtiff 写出 未压缩 RGBA TIFF

批处理写出文件时,format="tiff"format="tif" 均使用 .tif 后缀。

快速上手

图片文件 ↔ KTX2 文件

import alktx2

# png -> ktx2(更偏分发/体积)
alktx2.encode_image_to_ktx2(
    "a.png",
    "a.ktx2",
    codec="etc1s",
    quality=50,
    effort=3,
    srgb=True,
)

# ktx2 -> webp(方便快速预览)
alktx2.decode_ktx2_to_image(
    "a.ktx2",
    "a.webp",
    format="webp",
    quality=85,
    method=4,
)

bytes ↔ bytes(适合服务端/管线)

import alktx2

with open("a.webp", "rb") as f:
    img = f.read()

ktx2 = alktx2.encode_bytes_to_ktx2(
    img,
    mime="image/webp",
    codec="uastc",
    quality=80,
    effort=4,
    zstd_supercompression_level=6,
)

png_bytes, mime = alktx2.decode_ktx2_to_bytes(ktx2, format="png")
assert mime == "image/png"

便捷:mime 简写

encode_bytes_to_ktx2(..., mime=...) 支持两种写法:

  • 标准 MIME:image/pngimage/jpegimage/webp
  • 常见简写:pngjpgjpegwebptga 等(忽略大小写)

示例:

ktx2 = alktx2.encode_bytes_to_ktx2(img_bytes, mime="jpg")

TIFF 输入依赖 vendored libtiffzlib(见 build.md / THIRD_PARTY_NOTICES.md)。

批处理:目录 ↔ 目录(默认吃满 CPU)

当你需要一次性处理大量图片/ktx2 时,建议使用目录接口:

  • encode_images_dir_to_ktx2_dir():图片目录 → ktx2 目录
  • decode_ktx2_dir_to_images_dir():ktx2 目录 → 图片目录

它们支持:

  • recursive:是否递归子目录
  • suffixes:后缀过滤(忽略大小写,可写 png.png
  • prefix:文件名前缀过滤(忽略大小写)
  • workers:并发数(默认 CPU 核心数)

示例:

import alktx2

# 批量图片 -> ktx2(保持目录结构)
alktx2.encode_images_dir_to_ktx2_dir(
    "E:/textures",
    "E:/textures_ktx2",
    recursive=True,
    suffixes=["png", "jpg", "webp", "tga"],
    codec="etc1s",
    quality=50,
    effort=3,
    # 批处理建议 threads=1,通过 workers 吃满 CPU,避免过度抢占
    threads=1,
)

# 批量 ktx2 -> png
alktx2.decode_ktx2_dir_to_images_dir(
    "E:/textures_ktx2",
    "E:/textures_png_preview",
    recursive=True,
    suffixes=[".ktx2"],
    format="png",
    level=0,
)

并发建议(重要)

alktx2单次编码时就可以通过 threads=0 自动使用多核(默认即为 0,会尽量吃满 CPU)。 因此通常不需要、也不建议在 Python 层再叠加并发(否则很容易出现“过度抢占”导致更慢,甚至内存暴涨)。

  • 不建议使用 multiprocessing / 进程池:每个子进程内部都会再开多线程,整体 CPU/内存会被迅速打满。\n 如果你一定要做并发,优先使用本库提供的目录批处理接口(workers 控制并发)。\n
  • 不建议叠加 Python 3.14 的 free-threading 并发:本库的热点在 C++ 编码/解码,多线程在底层已经做了;\n Python 层再并发通常只会更混乱(尤其是同时 workers + threads=0 的情况下)。

参数总览(重点)

alktx2 目前对外暴露 5 个高集成度接口:

  • encode_image_to_ktx2() / encode_bytes_to_ktx2()\n
    • 核心是 BasisU 编码参数。\n
  • decode_ktx2_to_image() / decode_ktx2_to_bytes()\n
    • 核心是 输出图片编码参数(webp/jpg 质量等)。\n
  • inspect_ktx2()\n
    • 不解码像素,只读 KTX2 元信息(codec/块尺寸/色彩传递函数等)。\n

下面按“你最常用的图片 ↔ KTX2”场景解释关键参数(更完整的接口说明请以 src/alktx2/__init__.pyi 为准)。

编码:图片 → KTX2

1) 通用参数(推荐优先掌握)

  • codec:\n
    • etc1s:更小的体积(常用于下载分发),质量一般\n
    • uastc:更高质量(常用于近似无明显瑕疵),文件更大\n
    • astc_ldr / xuastc_ldr:ASTC/XUASTC LDR(当前默认块大小 6x6)
  • quality:1-100(质量/体积权衡)
  • effort:0-10(速度/最高可达质量权衡)
  • srgb / linear:互斥。\n
    • 色彩贴图(albedo/baseColor/UI):一般 srgb=True\n
    • 法线/金属度/粗糙度/高度等数据贴图:一般 linear=True
  • threads:0 自动;>0 指定线程池总线程数(更快但更吃 CPU)

2) KTX2(supercompression)

  • zstd_supercompression_level:对支持的模式启用 KTX2 的 Zstd supercompression。\n
    • 常用于 uastc:更利于分发(更小),但编码更慢。\n
    • None 表示使用库默认。

3) mipmap(当 mipmaps=True 时才生效)

  • mipmaps:是否自动生成 mipmap(强烈建议用于 3D 场景)
  • mip_wrap:生成 mip 时使用 wrap(True)或 clamp(False)
  • mip_filter:滤波器名称(空串=使用 basisu 默认)
  • mip_scale / mip_smallest_dimension:控制 mip 生成策略

4) UASTC(LDR 4x4)RDO(更小但更有损)

  • uastc_rdo_lambda:>0 开启 RDO;越大通常文件更小但质量下降。\n
    • 经验值:0.5 ~ 1.5 常作为起点。\n
  • uastc_rdo_dict_size:RDO 字典大小,None=默认

5) XUASTC profile(更偏“可转码速度 vs 压缩率”)

  • xuastc_syntax\"\" | \"zstd\" | \"hybrid\" | \"arith\"\n
    • zstd:转码更快,压缩率略差\n
    • arith:压缩率更好,转码更慢\n
    • hybrid:折中\n
  • xuastc_disable_subsets / xuastc_disable_dual_plane:偏向“转码到 BC7 更快/更稳定”的开关(可能牺牲质量)

6) Alpha/内容提示

  • check_for_alpha:是否检测输入 alpha(默认 True)
  • force_alpha:强制输出 alpha(默认 False)
  • renormalize:法线贴图 renormalize(默认 False)
  • y_flip:编码前沿 Y 翻转(默认 False)

解码:KTX2 → 图片(png/jpg/webp/tga/tiff)

  • formatpng / jpg / webp / tga / tif / tiff
  • quality:\n
    • jpg:1-100\n
    • webp:0-100(lossy)\n
  • lossless / near_lossless / method:仅对 webp 输出生效(更完整说明见 .pyi

原理(实现链路)

编码:图片 → KTX2(BasisU)

  1. 图片解码:\n
    • png/jpg/tga 等:stb_image 解码到 RGBA8\n
    • webp:libwebp 解码到 RGBA8\n
    • tiff:libtiffTIFFReadRGBAImageOriented 转为 RGBA8\n
  2. BasisU 编码:把 RGBA8 交给 basisu::basis_compressor,通过 basisu::basis_compressor_params 进行参数映射,产出 .ktx2。\n
  3. 返回/写盘:返回 bytes 或写出到文件。

解码:KTX2 → 图片

  1. KTX2 解析与转码:使用 basist::ktx2_transcoder,将 KTX2 解码/转码为 RGBA32(即 RGBA8 像素)。\n
  2. 图片编码:\n
    • png/jpg/tga:stb_image_write 编码\n
    • webp:libwebp 编码\n
    • tiff:libtiff 写出未压缩 RGBA\n

已知限制(当前版本)

  • KTX2 解码:默认解码 level=0, layer=0, face=0,但现在你可以在 decode_ktx2_to_*() 中显式指定:\n
    • level:mipmap level(0 为最大分辨率)\n
    • layer:texture array / video frame 的层索引(如果 inspect_ktx2() 返回 layers=0,则有效层数视为 1)\n
    • face:cubemap 面(2D=0;cubemap=0..5)\n
    • 这使得后续扩展到批量导出/更多结构变得更自然。\n
  • HDR/特殊 DFD:当前以 LDR/常见用途为主,后续会逐步补齐 HDR/更多 DFD 解释与更强的格式兼容性提示。

发布到 PyPI(多版本 wheel)

本地一键构建 Windows / Linux manylinux(cp311–cp314)并可选上传:见 scripts/README.md(命令示例:.\scripts\build_local_wheels.ps1 -UploadToPyPI)。

inspect_ktx2() 字段说明(简要)

inspect_ktx2() 返回的字典包含:

  • 基础结构width/height/levels/layers/faces
  • alpha 与 transfer functionhas_alpha/is_srgb
  • BasisU codecbasis_tex_format(枚举值)与 basis_tex_format_name(可读字符串)
  • 类型判断is_hdr/is_etc1s/is_uastc/is_astc_ldr/is_xuastc_ldr
  • 块尺寸block_width/block_height(例如 ETC1S/UASTC 常见为 4x4)
  • DFD(Data Format Descriptor)摘要dfd_color_model/dfd_transfer_func/dfd_channel_id0/dfd_channel_id1\n 这些字段用于快速判断数据语义(例如是否 sRGB、通道映射等)。更深入解释后续会补充。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

alktx2-0.1.6-cp314-cp314-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.14Windows x86-64

alktx2-0.1.6-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

alktx2-0.1.6-cp313-cp313-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.13Windows x86-64

alktx2-0.1.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

alktx2-0.1.6-cp312-cp312-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.12Windows x86-64

alktx2-0.1.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

alktx2-0.1.6-cp311-cp311-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.11Windows x86-64

alktx2-0.1.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file alktx2-0.1.6-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: alktx2-0.1.6-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for alktx2-0.1.6-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 193e8a9fe78975307e94948075a3df52ba1dae27f1d88ae518d2f2c7b53e11c9
MD5 badd0a527dfd03fecb9d0ea0526f0d5f
BLAKE2b-256 52de9b4b53a2db06165ad3d0b5c6d5cdee931a68e32de1591c2bdca402d1b2c7

See more details on using hashes here.

File details

Details for the file alktx2-0.1.6-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for alktx2-0.1.6-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7d4c846ee1776e4e8892c5244622de5d9ee71d2755be3baf5f97a5ceb7fba9ea
MD5 824abfc931dd280f11a4415e0000c6a8
BLAKE2b-256 896f0e2a4305543f08a930d1785e4e9ad020726a75022cd4dd12c72c214bece9

See more details on using hashes here.

File details

Details for the file alktx2-0.1.6-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: alktx2-0.1.6-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for alktx2-0.1.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 67b104a089ae98e52825fa386632ecca59e6b8ae4aa4e574c4d218041e3739b9
MD5 eceddbba259a79b1b9e6c5a3414b6772
BLAKE2b-256 f49ec2bc9c2126cd1f11769f316e9025d08a3f592b8ed1e58cf91ff201875e1e

See more details on using hashes here.

File details

Details for the file alktx2-0.1.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for alktx2-0.1.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8ac5b340e8319012f533264e16c63a90d3bf616b28d6271595b263f03c467cef
MD5 b3ece3d749970ce4b2b23906a7b005fe
BLAKE2b-256 236a12772ff9275fb4cd0c2eebba2feec22f41021f49fde6007b1880b198f6a4

See more details on using hashes here.

File details

Details for the file alktx2-0.1.6-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: alktx2-0.1.6-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for alktx2-0.1.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 6d1069d9b2b6c7d749caba0541a264801fed6e26e4637e0d62df92cc69cfa667
MD5 7e08181b6c06d08f2089549d9f1bd0ab
BLAKE2b-256 1782339e2380a8a9514000641d63763ed18850b9cb53b6a4537bb7d00a55aaa3

See more details on using hashes here.

File details

Details for the file alktx2-0.1.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for alktx2-0.1.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c94243301a2aa9b8a3766228681fda4487a3e91ab1605966b825d0ef6ddd104c
MD5 09d1ca32775aaa69951b8f8acc5d442a
BLAKE2b-256 d791e3c9a50a576233332df361a6ea67730c9dd9409af3911c635d7038bbd869

See more details on using hashes here.

File details

Details for the file alktx2-0.1.6-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: alktx2-0.1.6-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 1.9 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for alktx2-0.1.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 8e077abf9e2b216d83afd73046e5eee8b5606a917db62d0ca162ae1ecc0db9d0
MD5 84da858cd78af543a6c802a77f9f8f2a
BLAKE2b-256 5baae39048a76231d7efe140a3383658aeb47de17e3f7aea676c6b90d5d8e549

See more details on using hashes here.

File details

Details for the file alktx2-0.1.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for alktx2-0.1.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5257b8a70203894223f304491d492ffc704fbc1cd5700727f416b49d081cca31
MD5 660afbbb6cd880ed672e0a1e4ecf2126
BLAKE2b-256 9a008cee0f018f70f7cf6a3948c4818c2afff1920005707f924cc6133ef296b5

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