码迷,mamicode.com
首页 > 编程语言 > 详细

TexturePacker批处理python

时间:2016-05-20 19:50:40      阅读:633      评论:0      收藏:0      [点我收藏+]

标签:

TexturePacker版本 4.2.1

前言:因为水平有限,错误再所难免,望指正

1.安装TexturePacker Command Line Tool

1.1 Mac下安装 TexturePacker => Install Command Line Tool => Install

技术分享

技术分享

1.2 win下安装 必须配置环境变量。

2.TexturePacker命令说明(主要命令)

2.1 --texture-format

  --texture-format <id>         Sets the format for the textures.
                                The format type is automatically derived from the sheet's file name
                                if possible.
                                Available formats:
                                    png               PNG (32bit)
                                    png8              PNG (8bit indexed)
                                    pvr3              PowerVR Texture Format, PVR Version 3
                                    pvr3gz            PowerVR Texture Format, PVR Version 3, compressed with gzip
                                    pvr3ccz           PowerVR Texture Format, PVR Version 3, compressed with zlib, cocos2d header
                                    jpg               JPG image format, lossy compression, no transparency
                                    bmp               24 bit BMP
                                    tga               Targa Image Format
                                    tiff              Tagged Image File Format
                                    pkm               PKM image format, ETC1 compression
                                    webp              WebP lossless / lossy format
                                    atf               Adobe Texture Format
                                    pvr2              PowerVR Texture Format, PVR Version 2, deprecated
                                    pvr2gz            PowerVR Texture Format, PVR Version 2, compressed with gzip, deprecated
                                    pvr2ccz           PowerVR Texture Format, PVR Version 2, compressed with zlib, cocos2d header, deprecate
纹理输出格式,常用的 png,png8,pvr3ccz,pkm,jpg。  

png全平台都适用, 

png8文件小但效果比起png来说有一些损失,并且对一些图片渐变处理效果损失更加明显,如果效果看不过去还是用png吧

pvr3ccz pvr3ccz是pvr2ccz升级版,添加了一些功能,可以使用其他像素格式比如 ETC1,pvr2ccz 以前是不支持的。

pkm 主要用于ETC1
jpg 由于没有alpha通道,所以常用于背景图,压缩到80%的效果和原图差距不大,但压缩比例很吓人,文件小效果好,目前背景没有alpha通道的就用的这个。由于在cocos2d 2.x版本上加载速度慢,并且消耗内存大,根据实际情况选用。

2.2 --opt 像素格式

--opt <pixelformat>           Optimized output for given pixel formats. Supported formats are:
                                    RGBA8888           32bit, 8bit/channel, 8bit transparency
                                    BGRA8888           32bit, 8bit/channel, 8bit transparency
                                    RGBA4444           16bit, 4bit/channel, 4bit transparency
                                    RGB888             24bit, 8bit/channel, no transparency
                                    RGB565             16bit, 5bit red, 6bit green, 5bit blue, no transparancy
                                    RGBA5551           16bit, 5bit/channel, 1bit transparancy
                                    RGBA5555           20bit, 5bit/channel, 5bit transparancy
                                    PVRTCI_2BPP_RGB    PVRTC compression, 2bit per pixel
                                    PVRTCI_4BPP_RGB    PVRTC compression, 4bit per pixel
                                    PVRTCI_2BPP_RGBA   PVRTC compression, 2bit per pixel
                                    PVRTCI_4BPP_RGBA   PVRTC compression, 4bit per pixel
                                    PVRTCII_2BPP       PVRTC2 compression, 2bit per pixel
                                    PVRTCII_4BPP       PVRTC2 compression, 4bit per pixel
                                    ALPHA              8bit transparency
                                    ALPHA_INTENSITY    8bit intensity, 8bit transparency
                                    ETC1_RGB           ETC1 compression
                                    ETC1_A             ETC1 Alpha channel only
                                    ETC1_RGB_A         ETC1 RGB + ETC1 Alpha
                                    ETC2_RGB           ETC2 compression
                                    ETC2_RGBA          ETC2 Alpha
                                    DXT1               Compressed with DXT1, 1 bit transparency
                                    DXT5               Compressed with DXT5, transparency
                                    ATF_RGB            ETC1+DXT1+PVRTC4, no transparency
                                    ATF_RGBA           ETC1/ETC1+DXT5+PVRTC4, no transparency
像素格式常用 RGBA8888,RGBA4444,RGB888,RGB565,RGBA5551,RGBA5555,PVRTCI_4BPP_RGBA,ETC1_RGB,ERC1_A。

RGBA8888 效果最好消耗内存比较大。 内存消耗32bpp

RGBA4444 效果比起RGBA8888在某些图片上丢失得有点大。 但消耗内存是 RGBA8888的一半。 内存消耗16bpp

RGB888 少个alpha通道,内存占用是 RGBA8888 的 四分之三。内存消耗24bpp

RGB565 效果上比 RGB888差一些,但对于有些图片还是可以接受的, 但没有alpha通道。内存消耗16bpp

RGBA5551 对于透明渐变少的可以采用效果 比 RGBA4444好。内存消耗16bpp

RGBA5555 alpha通道增加 自然透明效果好了,但内存消耗也增加了。 仙存消耗24bpp

PVRTCI_4BPP_RGBA 效果上比RGBA8888要差一些,但内存消耗极小,加载速度也快,渲染速度快,在ios上完全是可以接受的,强烈建议ios 用这个格式。内存消耗4bpp

ETC1_RGB 消耗内存极小,加载速度比RGBA8888快,渲染速度快,效果也比较不错,但没有alpha通道,这也是ETC1被一直吐槽的点,所以后来有个ETC2。内存消耗4bpp

ETC1_A 和ETC1_RGB 组合在一起用,给ETC1_RGB提供alpha通道,达到透明效果,建议android可以选用这种方法来处理ETC1没有alpha通道的缺点。内存消耗4bpp

ETC1_RGB、ETC1_A可以通过shader对纹理进行两次采集处理达到RGBA的效果,这里就不讨论这个问题了。


内存消耗计算方式 :假如一张图是 1024 * 888

ETC1内存消耗 = 1024 * 888 * 4bpp / 8 = 454656byte = 444KB

PVRTCI_4BPP_RGBA内存消耗 = 1024 * 888 * 4bpp / 8 = 454656byte = 444KB

RGB888内存消耗 = 1024 * 888 * 24bpp / 8 = 2727936byte = 2664KB

RGBA8888内存消耗 = 1024 * 888 * 32bpp / 8 = 3637248byte = 3552KB

RGBA4444内存消耗 = 1024 * 888 * 16bpp / 8 = 1818624byte = 1776KB

2.3 --data 输出纹理文件的信息数据路径 plist

  --data <filename>             Name of the data file to write

2.4 --sheet 输出图集路径

  --sheet <filename>            Name of the sheet to write, see texture-format for formats available

2.5 --dither-type 颜色抖动

  --dither-type <dithertype>    Dithering to improve quality of color reduced images
                                    NearestNeighbour      no dithering
                                    Linear                no dithering
                                    FloydSteinberg        Floyd Steinberg, no alpha
                                    FloydSteinbergAlpha   Floyd Steinberg, with alpha
                                    Atkinson              Atkinson, no alpha
                                    AtkinsonAlpha         Atkinson, alpha
                                    PngQuantLow           PNG-8 only: minimum dithering
                                    PngQuantMedium        PNG-8 only: medium dithering
                                    PngQuantHigh          PNG-8 only: strong dithering

对图片颜色进行一些插值处理。不同参数算法不同。 

PngQuantLow,PngQuantMedium,PngQuantHigh选用PNG8 可以试试这三个参数。用了比原PNG8好 

2.6 --format
--format <format>             Format to write, default is cocos2d
                                Available formats:
                                    2dtoolkit           2D Toolkit exporter
                                    andengine           Format for AndEngine
                                    agk                 Format for AppGameKit
                                    batterytech         BatteryTech Exporter
                                    bhive               Format for BHive
                                    caat                Exporter for CAAT - the Canvas Advanced Animation Toolkit
                                    cegui               Format for CEGUI / OGRE
                                    cocos2d             plist format version 3 for cocos2d
                                    cocos2d-v2          old plist format version 2 for cocos2d (deprecated)
                                    cocos2d-x           plist format version 3 for cocos2d-x with polygon packing
                                    corona-imagesheet   Exporter for Corona(TM) SDK using new image sheet format.
                                    css                 css format for web design
                                    easeljs             Exporter for EaselJS.
                                    gideros             Format for Gideros
                                    json-array          Text file for json/html as array
                                    json                Text file for json/html as hash
                                    kwik                Exporter for Kwik using new image sheet format.
                                    less                Creates a LESS file that can be incorporated into a sprites arrangement
                                    libgdx              text file for lib GDX
                                    libRocket           Exporter to demonstrate how to crate your own exporters
                                    melonjs             Data file for MelonJS
                                    moai                Format for Moai
                                    molecule            Exporter for Molecule Framework
                                    monogame            Input format for the MonoGame TexturePacker Importer
                                    orx                 Orx Exporter
                                    panda               Exporter for Panda Engine
                                    phaser-json-array   JSON array data for Phaser
                                    phaser-json-hash    JSON hash data for Phaser
                                    pixijs              Data file for PixiJS
                                    sass-mixins         Exporter for SASS.
                                    shiva3d-jpsprite    Shiva3D with JPSprite extension
                                    shiva3d             Exporter for Shiva3D.
                                    slick2d             Format for Slick2D
                                    sparrow             xml file for Sparrow/Starling SDK
                                    spine               text file for Spine
                                    spritesheet-only    Exports only the sprite sheet without data file
                                    spritekit           plist format for SpriteKit, Objective-C header file
                                    spritekit-swift     plist format for SpriteKit, with swift class file
                                    spriter             JSON file for Spriter
                                    spritestudio        OPTPiX SpriteStudio 5 CellMap File.
                                    tresensa            Exporter for TreSensa TGE.
                                    uikit               Exporter for UIKit
                                    unity               Text file for Unity(R), json format with .txt ending
                                    unity-texture2d     Input format for the Unity(R) TexturePacker Importer
                                    unreal-paper2d      Format for UnrealEngine / Paper2d
                                    vplay               JSON file for V-Play engine
                                    wave-engine-1       WaveEngine Sprite Sheet
                                    x2d                 Export to x2d engine format.
                                    xml                 Generic XML format
                                    plain               Exporter to demonstrate how to crate your own exporters
纹理打包方式

cocos2d-x 新版本的多边形打包方式,目前cocos studio 不支持

cocos2d-v2 老版本的打包方式,现在已经被废弃,但我还是用的这个。

cocos2d 新版本的打包方式 目前cocos studio 不支持

其他方式比如unity,unreal,spritekit如果有用的可以自行研究


2.7 --multipack 多图集打包

--multipack                   Create multiple sprite sheets if not all sprites match into a single one
避免一个图集包含不完所以精灵,开起则会生成多张图集。但要在 --data --sheet 加入 {n}  索引会从0开始

2.8 --maxrects-heuristics

    MaxRects
      --maxrects-heuristics     Heuristic for MaxRects algorithm
                                    Best              Best
                                    ShortSideFit      ShortSideFit
                                    LongSideFit       LongSideFit
                                    AreaFit           AreaFit
                                    BottomLeft        BottomLeft
                                    ContactPoint      ContactPoint

maxrect的算法方式


2.9 --enable-rotation 精灵旋转

--enable-rotation             Enables rotation of sprites (overriding file format's defaults)
开起旋转,得到更小的图集


2.10 --trim-mode 透明像素处理

  --trim-mode <value>           Remove transparent parts of a sprite to shrink atlas size and speed up rendering
                                    None              Keep transparent pixels
                                    Trim              Remove transparent pixels, use original size.
                                    Crop              Remove transparent pixels, use trimmed size, flush position.
                                    CropKeepPos       Remove transparent pixels, use trimmed size, keep position.
                                    Polygon           Approximate sprite contour with polygon path.

常用Trim。

Trim 删除图片透明像素,用原图片像素大小,在帧动画中常用,

None 保证原样大小,和透明像素。 不建议用。图集大。

Crop 删除图片透明像素,用删除后的图片像素大小,位置改变

CropKeepPos 删除图片透明像素,用删除后的图片像素大小,位置不变

Polygon 多边形方式,得到图集更小,目前cocos2d 也不支持多边形精灵。


建议使用Trim,其他用处不大。

2.11 --basic-sort-by 和 --basic-order 

    Basic
      --basic-sort-by           Sort order for the sprite list
                                    Best              Best
                                    Name              Name
                                    Width             Width
                                    Height            Height
                                    Area              Area
                                    Circumference     Circumference
      --basic-order             Sorting direction
                                    Ascending         Ascending
                                    Descending        Descending

选用 Name 和 Ascending就行了。这个我觉得不用多做了解,按名称升序方式排序在 list中

2.12 --max-size 最大纹理图集大小

--max-size <int>              Sets the maximum width and height for the texture in auto size mode, default is 2048

建议使用2048,最大不超过4096,各个机型支持图集大小有区别。 以前

2.13 --size-constraints 图集高宽约束方式

  --size-constraints <value>    Restrict sizes
                                    POT               Power of 2 (2,4,8,16,32,...)
                                    WordAligned       Texture width is multiple of 2 (for 16-bit formats)
                                    AnySize           Any size
POT 2次幂方式

AnySize 任意大小

2.14  --force-publish

--force-publish               Ignore smart update hash and force re-publishing of the files

强制重新再次生成,忽略智能更新,废弃以前的 --smart-update

2.15 --shape-padding 和 --border-padding

  --shape-padding <int>         Sets a padding around each shape, value is in pixels, default is 2
  --border-padding <int>        Sets a padding around each the border, value is in pixels, default is 2

--shape-padding 边框填充像素

--border-padding 相当于精灵之间的间距像素


2.16  --scale 和 --scale-mode精灵缩放

  --scale <float>               Scales all images before creating the sheet. E.g. use 0.5 for half size
  --scale-mode <mode>           Use mode for scaling:
                                    Smooth            Smooth
                                    Fast              Fast (Nearest Neighbor)
                                    Scale2x           Scale2x (fixed 2x upscaling)
                                    Scale3x           Scale3x (fixed 3x upscaling)
                                    Scale4x           Scale4x (fixed 4x upscaling)
                                    Eagle             Eagle2x (fixed 2x upscaling)
                                    Hq2x              Hq2x (fixed 2x upscaling)

主要用于不同 分辨率的机型和处理纹理大小,如果只用一套资源来适配。不用关心。scale 设为1就行

2.17  --replace 正则表达式

  --replace <regexp>=<string>   Replaces matching parts of the sprite's name with <string>
                                Uses full regular expressions, make sure to escape the expression
正则表达式来修改精灵名,--replace ^={sheetName}_ win和mac得到的结果不一样。

3. python 批处理

#!/usr/bin/python  
#encoding=utf-8  
import io
import os
import sys  
import hashlib  
import string  
import re




rootPath = os.path.abspath(os.path.join(sys.argv[0], os.pardir))

# # input paths
ImageDir= os.path.join(rootPath, "input")

# temporary path to place the sprite sheets
OutputDir = os.path.join(rootPath, "output")

#PVRTC4
OutputDirPVRTC4 = os.path.join(OutputDir, "PVRTC4") #ios 中PVRTC4输出目录
#ETC
OutputDirETC =  os.path.join(OutputDir, "ETC") #android 中 ETC输出目录
#PNG
OutputDirPNG =  os.path.join(OutputDir, "PNG") #通用 中 PNG输出目录

OutputDirPNG8 =  os.path.join(OutputDir, "PNG8") #通用 中 PNG输出目录

# # path of the texture packer command line tool
TP="TexturePacker"


print("ImageDir = " + ImageDir)
print("OutputDir = " + OutputDir)
print("OutputDirPVRTC4 = " + OutputDirPVRTC4)
print("OutputDirETC = " + OutputDirETC)
print("OutputDirPNG = " + OutputDirPNG)
print("OutputDirPNG8 = " + OutputDirPNG8)
print("TP = " + TP)


#文件输出目录
def createPath(cPath):
    if not os.path.isdir(cPath):
        os.mkdir(cPath)


# --trim-sprite-names  去除png等后缀
# --multipack 多图片打包开起,避免资源图太多,生成图集包含不完全,开起则会生成多张图集。
# --maxrects-heuristics macrect的算法  参数 Best ShortSideFit LongSideFit AreaFit BottomLeft ContactPoint
# --enable-rotation 开起旋转,计算rect时如果旋转将会使用更优的算法来处理,得到更小的图集
# --border-padding 精灵之间的间距
# --shape-padding 精灵形状填充
# --trim-mode Trim 删除透明像素,大下使用原始大小。 参数 None Trim Crop CropKeepPos Polygon
# --basic-sort-by Name  按名称排序
# --basic-order Ascending 升序
# --texture-format 纹理格式
# --data 输出纹理文件的信息数据路径 plist
# --sheet 输出图集路径 png
# --scale 1 缩放比例 主要用于低分辨率的机子多资源适配。
# --max-size 最大图片像素 一般我是用的2048,超过2048以前的有些android机型不支持。
# --size-constraints 结纹理进行大小格式化,AnySize 任何大小 POT 使用2次幂 WordAligned
# --replace 正则表达式,用于修改plist加载后的名称
# --pvr-quality PVRTC 纹理质量
# --force-squared 强制使用方形
# --etc1-quality ETC 纹理质量
def pack_textures(inputPath, outputPath, opt, scale, maxSize, sheetSuffix, textureFormat, sizeConstraints, sheetName, otherParams, fileNameSuffix):
    packCommand = TP +         " --multipack"         " --format cocos2d-v2"         " --maxrects-heuristics best"         " --enable-rotation"         " --shape-padding 2"         " --border-padding 0"         " --trim-mode Trim"         " --basic-sort-by Name"         " --basic-order Ascending"         " --texture-format {textureFormat}"         " --data {outputSheetNamePath}{fileNameSuffix}.plist"         " --sheet {outputSheetNamePath}{fileNameSuffix}.{sheetSuffix}"         " --scale {scale}"         " --max-size {maxSize}"         " --opt {opt}"         " --size-constraints {sizeConstraints}"         " {inputPath}"         " {otherParams}"


    # win 和 mac 上处理正则表达式结果不一样
    if sys.platform == "win32":
        packCommand = packCommand + " --replace (.png)$="             " --replace \\b={sheetName}_"             " --replace {sheetName}_$=.png"
    else:
        packCommand = packCommand + " --replace ^={sheetName}_"


    packCommand = packCommand.format(
        textureFormat=textureFormat,
        outputSheetNamePath=os.path.join(outputPath,sheetName) + "_{n}",
        sheetName=sheetName,
        sheetSuffix=sheetSuffix,
        scale=scale,
        maxSize=maxSize,
        opt=opt,
        sizeConstraints=sizeConstraints,
        inputPath=inputPath,
        otherParams=otherParams,
        fileNameSuffix=fileNameSuffix)
    os.system(packCommand)

if __name__ == '__main__':
    createPath(OutputDir)
    createPath(OutputDirPVRTC4)
    createPath(OutputDirETC)
    createPath(OutputDirPNG)
    createPath(OutputDirPNG8)
    for sheet in os.listdir(ImageDir):
        iPath = os.path.join(ImageDir, sheet)
        if os.path.isdir(iPath): 
            pack_textures(iPath,OutputDirPVRTC4,'PVRTCI_4BPP_RGBA',1,2048,'pvr.ccz',"pvr3ccz","POT",sheet,"--pvr-quality best --force-squared", "")
            pack_textures(iPath,OutputDirETC,'ETC1_RGB',1,2048,'pkm',"pkm","AnySize",sheet,"--etc1-quality high-perceptual", "")
            pack_textures(iPath,OutputDirETC,'ETC1_A',1,2048,'pkm',"pkm","AnySize",sheet,"--etc1-quality high-perceptual", "_alpha")
            pack_textures(iPath,OutputDirPNG,'RGBA8888',1,2048,'png',"png","AnySize",sheet,"--png-opt-level 7", "")
            pack_textures(iPath,OutputDirPNG8,'RGBA8888',1,2048,'png',"png8","AnySize",sheet,"--png-opt-level 7 --dither-type PngQuantHigh", "")








TexturePacker批处理python

标签:

原文地址:http://blog.csdn.net/lovehappy108/article/details/51462831

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!