-
Notifications
You must be signed in to change notification settings - Fork 0
/
pillow_processor.py
106 lines (78 loc) · 2.89 KB
/
pillow_processor.py
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
import os
from PIL import Image, ImageOps, UnidentifiedImageError
from def_globals import *
def ImageConvert(img, dest_path):
'''
CorruptImageError
'''
if (DEBUG_DISABLE_IMAGE_CONV): return # Debug disable.
if (not os.path.isfile(img)):
logging.error("\n\t\t\tPillow not found texture: " + img + "\n")
return
filename, file_extension = os.path.splitext(os.path.basename(img))
new_filename = convert_suffixes_to_unigine(filename)
try:
original = Image.open(img)
black = Image.new('RGB', original.size, (0, 0, 0)).convert('L')
r, g, b, a = black, black, black, black
if (original.mode == "RGB"):
r, g, b = original.split()
else:
r, g, b, a = original.split()
exported_file = os.path.join(dest_path, new_filename + ".tga")
if (filename.endswith("_diff")):
# albedo (RGB, RGB+A)
if (original.mode == "RGB"):
converted = Image.merge('RGB', (r, g, b))
converted.save(exported_file, compression = 'tga_rle')
else:
converted = Image.merge('RGBA', (r, g, b, a))
converted.save(exported_file, compression = 'tga_rle')
pass
if (filename.endswith("_ddna")):
# normalmap (RG)
converted = Image.merge('RGB', (r, g, b))
converted.save(exported_file, compression = 'tga_rle')
# shading (R-metalness, G-roughness, B-specular)
spec_img = None
spec_path = img[:-8] + "spec.tif"
if (os.path.exists(spec_path)):
spec_img = Image.open(spec_path).convert('L')
a = ImageOps.invert(a)
if (spec_img):
gloss_img = Image.merge('RGB', (black, a, spec_img))
else:
gloss_img = Image.merge('RGB', (black, a, black))
exported_file = exported_file.replace("_n", "_sh")
gloss_img.save(exported_file, compression = 'tga_rle')
pass
if (filename.endswith("_spec")):
pass
if (filename.endswith("_sss") or filename.endswith("_mask") or filename.endswith("_detail")):
converted = Image.merge('RGB', (r, g, b))
converted.save(exported_file, compression = 'tga_rle')
pass
if (filename.endswith("_blend")):
if (original.mode == "RGB"):
converted = Image.merge('RGB', (r, g, b))
converted.save(exported_file, compression = 'tga_rle')
pass
if (filename.endswith("_detail")):
if (original.mode == "RGB"):
converted = Image.merge('RGB', (r, g, b))
converted.save(exported_file, compression = 'tga_rle')
else:
converted = Image.merge('RGBA', (r, g, b, a))
converted.save(exported_file, compression = 'tga_rle')
pass
if (filename.endswith("_displ")):
if (original.mode == "RGBA" or original.mode == "RGBX"):
converted = Image.merge('RGB', (a, a, a))
converted.save(exported_file, compression = 'tga_rle')
pass
except UnidentifiedImageError as e:
original = None
except Exception as e:
if (original):
logging.error("\n\t\t\tPillow exception on texture: " + img + "\n\t\t\t" + str(e) + "\n\t\t\tFormat: " + original.format + " Mode:" + original.mode + "\n")
pass