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.5-cp314-cp314-win_amd64.whl (2.0 MB view details)

Uploaded CPython 3.14Windows x86-64

alktx2-0.1.5-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.5-cp313-cp313-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.13Windows x86-64

alktx2-0.1.5-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.5-cp312-cp312-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.12Windows x86-64

alktx2-0.1.5-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.5-cp311-cp311-win_amd64.whl (1.9 MB view details)

Uploaded CPython 3.11Windows x86-64

alktx2-0.1.5-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.5-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: alktx2-0.1.5-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.5-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 ed061105a59febef46e8e9352ea5bc5a14b9a2a98366be6d98f29083d8eaeab8
MD5 c8b045964de51be4b8f8d409381e7ad7
BLAKE2b-256 7327629e0fab1125c837c0519e092f9d68b56dadd68b8a7097d061b3ab90df23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for alktx2-0.1.5-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7daa278a25387a33d6e066f6eaeca08db97ad76c5f224132a28adcb38c50701a
MD5 17b3a83d49850b27c2f795f084b13f34
BLAKE2b-256 88d5ba138f1324e18de222731e04b46c685d0df4a95d1a6be86e912a1a6f6c34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alktx2-0.1.5-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.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 613adb0eaa012f4cb6522a140b305b9e8d0c385988ee28f58b28c28c7064bd1b
MD5 ce3d65607d7df2afa8cc5d46f644fd08
BLAKE2b-256 85cf4d3c19d0d50b8a84e7d81362583d09e05b32189b73d453033b5630d1930d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for alktx2-0.1.5-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 619faf3609545f9eea6e28bffb65cc40efc81c04507dd43f0d53406382f68749
MD5 5a2acac8fe66a97614a1bec475db6a2e
BLAKE2b-256 bbdc5d5012d1f6f538b0a17549d1aa2769c7107fd32e0e8fb991ef36eb7856ad

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alktx2-0.1.5-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.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f45c84e694bef7ff57a919f8b5839fdc0bd31e00c5c66ac77ba7066a2dfa07ca
MD5 f76c7ded4733bae5885036c427a8c9c0
BLAKE2b-256 b3a1a81add5fdf47a4d00daf8f7695123d1c3df3a84bc3f28f42f881618514a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for alktx2-0.1.5-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 05842519bb3464ac52815a54a04186e622d1ef66d62da2fa3a6951572ad4fd73
MD5 311381d0229419e84d3f52e93ed933f0
BLAKE2b-256 2348a8d101c46479db8fbf5c68b3ece2eb590d4292e74220d4f8030831a522c9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alktx2-0.1.5-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.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 00926d86ad8a77a5c93eeaec8d279082c2d039a480045b810e1cd0f310d96223
MD5 7be9089c0ea043ff7f7a6819c70d2606
BLAKE2b-256 2d9bd875ce20e690bda825eae8fedeb6396024cc19aaa7d8b92f7b29ea769f94

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for alktx2-0.1.5-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9d394472ab78d00f04226b91ba8b5fa109f63c9177d2ece9f404777d7b5c2730
MD5 d6d3cea622b8f63416ba8072d48151b3
BLAKE2b-256 da14c02480e6a263e52c2ce636e899301624b0df0ebd73ce0441ceab3b6d2947

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