difenduandada
2024-12-31 34abe6963b344c882358274957f4b992456fee40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/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("不认识这种参数签名")