#!/usr/bin/env python
|
# -*- coding: utf-8 -*-
|
|
import os
|
import os.path
|
import sys
|
import subprocess
|
import re
|
import math
|
import platform
|
|
image_info_regexp = re.compile("(.*)\s*(JPEG|PNG)\s*(\d{1,10})x(\d{1,10})")
|
|
tile_width_size = 256.0
|
tile_height_size = 256.0
|
|
systemtype = None
|
enc = "utf-8"
|
|
# 函数定义 开始
|
def r_help():
|
p("使用方法\n")
|
p("获取图像信息 r.py im|imageinfo [source]")
|
p("裁剪场景地图3 r.py gs|gen-scene [source] [destination] [mainType] [backType] [midType]")
|
p("剪裁场景地图2 r.py gs2|gen-scene2 [source] [destination] [mainType] [backType]")
|
|
p("-----------------------------------------------------------------------\n")
|
p("mt|modeltexture 使用方法:\n")
|
p("-avatar 指定处理avatar")
|
# p("-commontexture 指定处理commontexture")
|
# p("-mount 指定处理mount")
|
|
#处理3层地图的
|
#sourcePath 源路径
|
#destinationPath 目标路径
|
#mainType 前景曾层资源格式 jpg或者png
|
#backType 背景层资源格式
|
#midType 中间层资源格式
|
def r_gen_scene(sourcePath, destinationPath, backType, midType, mainType):
|
|
fold = os.path.split(sourcePath)[1]
|
p(fold)
|
|
files = os.listdir(sourcePath)
|
for f in files:
|
if os.path.isfile(os.path.join(sourcePath, f)):
|
subFileName = os.path.splitext(f)[0]
|
|
if subFileName == "blayer":
|
backendImagePath = os.path.join(sourcePath, f)
|
r_gen_scene_backend(backendImagePath, destinationPath, "blayer", backType)
|
|
if subFileName == "mlayer":
|
midendImagePath = os.path.join(sourcePath, f)
|
r_gen_scene_frontend(midendImagePath, destinationPath, "mlayer", midType)
|
|
if subFileName == "slayer":
|
frontendImagePath = os.path.join(sourcePath, f)
|
r_gen_scene_frontend(frontendImagePath, destinationPath, "slayer", mainType)
|
pass
|
|
#切割场景地图
|
#sourcePath 源路径 like ../地图数据/资源/10008
|
#destinationPath 目标路径 ../resources/res/scene/
|
def r_gen_scene2(sourcePath, destinationPath, mainType, backType):
|
fold = os.path.split(sourcePath)[1]
|
p(fold)
|
|
files = os.listdir(sourcePath)
|
for f in files:
|
if os.path.isfile(os.path.join(sourcePath, f)):
|
subFileName = os.path.splitext(f)[0]
|
if subFileName == fold + "1":
|
backendImagePath = os.path.join(sourcePath, f)
|
r_gen_scene_backend(backendImagePath, destinationPath, "blayer", backType)
|
|
if subFileName == fold + "2":
|
frontendImagePath = os.path.join(sourcePath, f)
|
r_gen_scene_frontend(frontendImagePath, destinationPath, "slayer", mainType)
|
pass
|
|
#切主场景的地图,
|
def r_gen_scene3(sourcePath, destinationPath, mainType):
|
fold = os.path.split(sourcePath)[1]
|
p(fold)
|
|
files = os.listdir(sourcePath)
|
for f in files:
|
if os.path.isfile(os.path.join(sourcePath, f)):
|
subFileName = os.path.splitext(f)[0]
|
if subFileName == "1":
|
frontendImagePath = os.path.join(sourcePath, f)
|
r_gen_scene_frontend(frontendImagePath, destinationPath, "slayer", mainType)
|
pass
|
|
def r_gen_scene3_all(sourcePath, destinationPath):
|
|
imageInfo = r_get_image_info(sourcePath)
|
|
if None == imageInfo:
|
exit(9) # 没法获取图片信息
|
|
width = imageInfo["width"]
|
height = imageInfo["height"]
|
|
r_resize_image(sourcePath, destinationPath, width, height)
|
|
#前景层从左下角开始切
|
def r_gen_scene_frontend(sourcePath, destinationPath, subFolderName, backType):
|
if backType == "null": return
|
'''生成场景背景'''
|
imageInfo = r_get_image_info(sourcePath)
|
if None == imageInfo:
|
p('没有图像数据,无法继续处理')
|
exit(7)
|
|
print(imageInfo["path"] + " " + imageInfo['type'] + " " +str(imageInfo['width']) + "x" + str(imageInfo['height']))
|
|
# 准备图像所要切割的数目
|
width = float(imageInfo["width"])
|
height = float(imageInfo["height"])
|
tile_col_num = int(math.ceil(width / tile_width_size))
|
tile_row_num = int(math.ceil(height / tile_height_size))
|
|
p("切割列行数:" + str(tile_col_num) + "x" + str(tile_row_num))
|
|
if not os.path.isdir(destinationPath):
|
os.mkdir(destinationPath)
|
if not os.path.isdir(destinationPath + "/" + subFolderName):
|
os.mkdir(destinationPath + "/" + subFolderName)
|
|
# 剪出文件名
|
sourceFileName = os.path.splitext(os.path.basename(sourcePath))[0]
|
|
for col in range(0, tile_col_num):
|
for row in range(0, tile_row_num):
|
if backType == "jpg":
|
targetJPGFileName = destinationPath + "/" + subFolderName + "/" + str(col) + "_" + str(row) + ".jpg"
|
else:
|
targetJPGFileName = None
|
|
if backType == "png":
|
targetPNGFileName = destinationPath + "/" + subFolderName + "/" + str(col) + "_" + str(row) + ".png"
|
else:
|
targetPNGFileName = None
|
r_cut_tile(sourcePath, targetPNGFileName, targetJPGFileName, tile_width_size, tile_height_size, col * tile_width_size, height - (row+1) * tile_height_size)
|
|
|
#背景层从左上角开始切
|
def r_gen_scene_backend(sourcePath, destinationPath, subFolderName, mainType):
|
if mainType == "null": return
|
'''生成场景背景'''
|
imageInfo = r_get_image_info(sourcePath)
|
if None == imageInfo:
|
p('没有图像数据,无法继续处理')
|
exit(7)
|
|
print(imageInfo["path"] + " " + imageInfo['type'] + " " +str(imageInfo['width']) + "x" + str(imageInfo['height']))
|
|
# 准备图像所要切割的数目
|
width = float(imageInfo["width"])
|
height = float(imageInfo["height"])
|
tile_col_num = int(math.ceil(width / tile_width_size))
|
tile_row_num = int(math.ceil(height / tile_height_size))
|
|
p("切割列行数:" + str(tile_col_num) + "x" + str(tile_row_num))
|
|
if not os.path.isdir(destinationPath):
|
os.mkdir(destinationPath)
|
if not os.path.isdir(destinationPath + "/" + subFolderName):
|
os.mkdir(destinationPath + "/" + subFolderName)
|
|
# 剪出文件名
|
sourceFileName = os.path.splitext(os.path.basename(sourcePath))[0]
|
|
for col in range(0, tile_col_num):
|
for row in range(0, tile_row_num):
|
|
if mainType == "jpg":
|
targetJPGFileName = destinationPath + "/" + subFolderName + "/" + str(col) + "_" + str(row) + ".jpg"
|
else:
|
targetJPGFileName = None
|
|
if mainType == "png":
|
targetPNGFileName = destinationPath + "/" + subFolderName + "/" + str(col) + "_" + str(row) + ".png"
|
else:
|
targetPNGFileName = None
|
|
r_cut_tile(sourcePath, targetPNGFileName, targetJPGFileName, tile_width_size, tile_height_size, col * tile_width_size, row * tile_height_size)
|
|
|
def r_get_image_info(sourcePath):
|
'''通过GraphicsMagick的identify来获取图片的详细信息'''
|
try:
|
# 调用GraphicsMagick来获取图片的长宽及类型数据
|
output = subprocess.check_output("gm identify " + sourcePath, shell=True)
|
|
regexpResult = image_info_regexp.findall(output)
|
path = regexpResult[0][0].strip()
|
imageType = regexpResult[0][1].strip()
|
width = int(regexpResult[0][2])
|
height = int(regexpResult[0][3])
|
return {"width":width, "height":height, "type":imageType, "path":sourcePath}
|
|
except subprocess.CalledProcessError:
|
p("获取图片信息失败:" + sourcePath)
|
return None
|
|
return None
|
|
def r_resize_image(sourcePath, destinationPath, w = 0, h = 0):
|
imageInfo = r_get_image_info(sourcePath)
|
|
if None == imageInfo:
|
exit(9) # 没法获取图片信息
|
|
width = imageInfo["width"]
|
height = imageInfo["height"]
|
scale = max(width/721.0, height/459.0)
|
p("缩放默认比例:" + str(width) + "x" + str(height) + "==>" + str(scale))
|
|
if w == 0:
|
width = width / scale
|
else:
|
width = w
|
|
if h == 0:
|
height = height / scale
|
else:
|
height = h
|
|
resizeCMD = "gm convert " + sourcePath + " -resize " + str(int(width)) + "x" + str(int(height)) + " +profile \"*\" " + destinationPath
|
print(resizeCMD)
|
result = subprocess.call(resizeCMD, shell=True)
|
if result != 0:
|
p("操作失败")
|
exit(911)
|
|
|
def r_cut_tile(sourcePath, pngPath, jpgPath, width, height, offsetX, offsetY):
|
if None != pngPath:
|
pngCMD = "gm convert " + sourcePath + " -crop " + str(int(width)) + "x" + str(int(height)) + "+" + str(int(offsetX)) + "+" + str(int(offsetY)) + " +profile \"*\" -colors 65536 " + pngPath
|
#pngCMD = "gm convert " + sourcePath + " -crop " + str(int(width)) + "x" + str(int(height)) + "+" + str(int(offsetX)) + "+" + str(int(offsetY)) + " -background transparent -extent " + str(int(width)) + "x" + str(int(height)) + " +profile \"*\" -colors 65536 " + pngPath
|
print(pngCMD)
|
result = subprocess.call(pngCMD, shell=True)
|
if result != 0:
|
p("操作PNG失败")
|
exit(911)
|
|
if None != jpgPath:
|
jpgCMD = "gm convert " + sourcePath + " -crop " + str(int(width)) + "x" + str(int(height)) + "+" + str(int(offsetX)) + "+" + str(int(offsetY)) + " +profile \"*\" " + jpgPath
|
print(jpgCMD)
|
result = subprocess.call(jpgCMD, shell=True)
|
if result != 0:
|
p("操作JPG失败")
|
exit(911)
|
|
|
def r_png2atf_commontexture(sourcePath):
|
list = os.listdir(sourcePath)
|
for line in list:
|
path = os.path.join(sourcePath, line)
|
postfix = os.path.splitext(path)[1].lower()
|
if (os.path.isdir(path)):
|
r_png2atf_commontexture(path)
|
else:
|
if os.path.isfile(path) and (postfix == ".tga"):
|
tga2pngCmd = "gm convert " + path + " " + os.path.splitext(path)[0] + ".png"
|
subprocess.call(tga2pngCmd, shell=True)
|
p("转换tga文件:" + path)
|
|
destPath = os.path.splitext(path)[0] + ".atf"
|
p("输出文件:" + destPath)
|
|
sourcePng = os.path.splitext(path)[0] + ".png"
|
subprocess.call(get_png2atf_cmd() + " -c d -r -q 0 -f 0 -n 0,0 -i " + sourcePng + " -o " + destPath, shell=True)
|
|
|
def r_png2atf_mount(sourcePath, destinationPath):
|
pass
|
|
|
def convert_one_modeltexture(sourcePath):
|
if os.path.isfile(sourcePath):
|
path = sourcePath
|
postfix = os.path.splitext(path)[1].lower()
|
if os.path.isfile(path) and (postfix == ".tga"):
|
p("===================\n" + "★开始转换贴图文件:" + path)
|
|
pngPath = os.path.splitext(path)[0] + ".png"
|
tga2pngCmd = "gm convert " + path + " " + pngPath
|
subprocess.call(tga2pngCmd, shell=True)
|
p("tga转png完成,src:" + path + ";dest:" + pngPath)
|
|
atfPath = os.path.splitext(path)[0] + ".atf"
|
png2AtfCmd = get_png2atf_cmd() + " -c d -r -q 0 -f 0 -n 0,0 -i " + pngPath + " -o " + atfPath
|
subprocess.call(png2AtfCmd, shell=True)
|
p("png转atf完成,src:" + pngPath + ";dest:" + atfPath)
|
|
p("★贴图文件转换完毕:" + path + "\n===================\n\n")
|
|
|
def convert_modeltexture(sourcePath):
|
if os.path.isdir(sourcePath):
|
files = os.listdir(sourcePath)
|
for f in files:
|
path = os.path.join(sourcePath, f)
|
postfix = os.path.splitext(path)[1].lower()
|
if (os.path.isdir(path)):
|
convert_modeltexture(path)
|
else:
|
convert_one_modeltexture(path)
|
else:
|
convert_one_modeltexture(sourcePath)
|
|
|
def get_png2atf_cmd():
|
if platform.system() == "Darwin":
|
return "./scripts/png2atf"
|
else:
|
return "./scripts/png2atf.exe"
|
|
|
def jpg2atf_for_battlebg(sourcePath):
|
if os.path.isfile(sourcePath):
|
path = sourcePath
|
postfix = os.path.splitext(path)[1].lower()
|
if postfix == ".jpg":
|
tmpJpgFile = os.path.splitext(path)[0] + "_t.jpg"
|
convert_to_power2_jpg_cmd = "gm convert " + path + " -thumbnail 1920x1080 -background gray -gravity center -extent 2048x2048 " + tmpJpgFile
|
subprocess.call(convert_to_power2_jpg_cmd, shell=True)
|
|
jpg2pngCmd = "gm convert " + tmpJpgFile + " " + os.path.splitext(path)[0] + ".png"
|
subprocess.call(jpg2pngCmd, shell=True)
|
p("将jpg转换为png:" + path)
|
|
# 调用外部png2atf来处理文件
|
sourcePng = os.path.splitext(path)[0] + ".png"
|
p("将png转换为atf:" + sourcePng)
|
|
destinationFilePath = os.path.splitext(path)[0] + ".atf"
|
subprocess.call(get_png2atf_cmd() + " -c d -r -q 0 -f 0 -n 0,0 -i " + sourcePng + " -o " + destinationFilePath, shell=True)
|
|
os.remove(sourcePng)
|
os.remove(tmpJpgFile)
|
|
|
def convert_battlebg(sourcePath):
|
if os.path.isdir(sourcePath):
|
files = os.listdir(sourcePath)
|
for f in files:
|
path = os.path.join(sourcePath, f)
|
postfix = os.path.splitext(path)[1].lower()
|
if (os.path.isdir(path)):
|
convert_battlebg(path)
|
else:
|
jpg2atf_for_battlebg(path)
|
else:
|
jpg2atf_for_battlebg(sourcePath)
|
|
|
def p(outputStr):
|
'''自动转换UTF并输出的函数'''
|
global systemtype, enc
|
if systemtype == None:
|
systemtype = platform.system()
|
if systemtype == "Darwin":
|
enc = "utf-8"
|
|
print(unicode(outputStr, enc)) # 这里因为是CYGWIN将输出转为gbk了
|
|
# 函数定义 结束
|
|
# main 函数
|
if __name__ == "__main__":
|
workingDir = "./"
|
|
# 一般没有参数都是想看看有什么可以用,所以就输出个help
|
if len(sys.argv) < 2:
|
r_help()
|
exit(0)
|
|
# 处理 imageinfo 参数
|
if sys.argv[1] == "im" or sys.argv[1] == "imageinfo":
|
|
if len(sys.argv) < 3:
|
p("不传源路径你要我转什么文件给你?")
|
exit(4)
|
|
result = r_get_image_info(sys.argv[2])
|
if None == result:
|
p('没有图像数据,无法继续处理')
|
exit(7)
|
|
print(result["path"])
|
print(result["type"])
|
print(result['width'])
|
print(result['height'])
|
|
# 处理gs参数
|
if sys.argv[1] == "gs" or sys.argv[1] == "gen-scene":
|
if len(sys.argv) < 7:
|
p("参数都不对,你想怎样?")
|
exit(4)
|
if sys.argv[7] :
|
tile_width_size = int(sys.argv[7])
|
tile_height_size = int(sys.argv[7])
|
|
r_gen_scene(sys.argv[2], sys.argv[3],sys.argv[4], sys.argv[5], sys.argv[6])
|
|
# 处理gs参数
|
elif sys.argv[1] == "gs2" or sys.argv[1] == "gen-scene2":
|
if len(sys.argv) < 6:
|
p("参数都不对,你想怎样?")
|
exit(4)
|
r_gen_scene2(sys.argv[2], sys.argv[3],sys.argv[4], sys.argv[5])
|
|
elif sys.argv[1] == "gs3":
|
if len(sys.argv) < 5:
|
p("参数都不对,你想怎样?")
|
exit(4)
|
r_gen_scene3(sys.argv[2], sys.argv[3], sys.argv[4])
|
|
elif sys.argv[1] == "resize":
|
if len(sys.argv) < 4:
|
p("参数数量不对")
|
exit(4)
|
|
if len(sys.argv) == 4:
|
r_resize_image(sys.argv[2], sys.argv[3])
|
elif len(sys.argv) == 5:
|
r_resize_image(sys.argv[4], sys.argv[4], int(sys.argv[2]), int(sys.argv[3]))
|
elif len(sys.argv) == 6:
|
r_resize_image(sys.argv[4], sys.argv[5], int(sys.argv[2]), int(sys.argv[3]))
|
else:
|
p("不认识这种参数签名")
|