Pillow - 用于图像处理。
PyTexturePacker - 用于生成纹理图集。
webp - 用于支持WebP格式的编码。

你可以使用pip来安装这些库:

pip install pillow pytexturepacker webp

下面是一个示例Python脚本,展示如何使用Pillow加载图像,使用PyTexturePacker打包,并保存为WebP格式

from PIL import Image
import pytexturepacker
import os
 
def pack_images_to_webp(input_dir, output_path, texture_size=(2048, 2048)):
    # 创建一个纹理图集配置
    config = pytexturepacker.TexturePackerConfig()
    config.format = 'webp'  # 设置输出格式为WebP
    config.max_width = texture_size[0]
    config.max_height = texture_size[1]
    config.trim_mode = 'Trim'  # 裁剪边缘空白
    config.padding = 2  # 在每个图像周围添加填充以避免纹理过滤问题
    config.shape_padding = True  # 在形状之间添加额外的填充以避免纹理过滤问题
    config.duplicate_padding = True  # 为重复的图像添加额外的填充以区分它们
    config.rotation = True  # 允许旋转图像以优化空间使用
    config.algorithm = 'Skyline'  # 使用Skyline算法进行打包以优化空间使用
    config.output_format = 'json'  # 输出JSON格式的数据以便于使用
    config.data_file = os.path.splitext(output_path)[0] + '.json'  # JSON数据文件路径
    config.texture_file = output_path  # 输出纹理文件路径
    config.scale = 1  # 缩放比例
    config.shape_alphabet = True  # 使用形状字母排序以提高渲染效率
    config.ignore_empty = True  # 忽略空的图像文件
    config.extrude = 0  # 扩展边缘像素以避免纹理过滤问题(默认为0)
    config.dither_image = False  # 不使用抖动图像(对于透明度较好的图像)
    config.premultiply_alpha = False  # 不预乘Alpha通道(对于透明度较好的图像)
    config.border_padding = 0  # 边框填充(默认为0)
    config.inner_padding = 1  # 内部填充(默认为1)
    config.pot = False  # 不强制使用2的幂大小(对于WebP通常是需要的)
    config.square = False  # 不强制生成正方形纹理图集(对于WebP通常是需要的)
    config.fast = False  # 不使用快速模式(对于WebP通常是需要的)
    config.allow_rotation = True  # 允许旋转以优化空间使用(对于WebP通常是需要的)
    config.force_square = False  # 不强制生成正方形纹理(对于WebP通常是需要的)
    config.alpha_threshold = 0  # Alpha阈值(默认为0)
    config.filter_min = 'linear'  # 最小过滤模式(默认为'linear')
    config.filter_mag = 'linear'  # 最大过滤模式(默认为'linear')
    config.premultiplied_alpha = False  # 不预乘Alpha通道(对于WebP通常是需要的)
    config.quality = 80  # 设置WebP质量(默认为80)
    config.method = 6  # 设置WebP压缩方法(默认为6)
    config.exact = False  # 不使用精确模式(对于WebP通常是需要的)
    config.premultiply_alpha = False  # 不预乘Alpha通道(对于WebP通常是需要的)
    config.dithering_strength = 100  # 设置抖动强度(默认为100)
    config.premultiplied_alpha =

标签: none

添加新评论