-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
164 lines (138 loc) · 5.41 KB
/
main.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
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
import json
import os
import itertools
from psd_tools import PSDImage
list_layer_parent = []
found_2_dog = 0
class _Const:
def __init__(self, file_psd):
self.psd = file_psd
def find_parent(layer):
grant = layer.parent
if grant is None:
return []
list_parent = []
count_parent = 0
for parent in grant:
if parent.kind == 'group' and parent.name.lower() != 'background':
count_parent += 1
list_parent.append(parent)
elif parent.name.lower() == 'background':
parent.visible = True
if count_parent > 0:
return list_parent
return []
def set_visible(group, mode, root):
name = ""
for layer in group:
if layer.name.lower() != 'background' and layer.kind == 'group':
layer.visible = mode
name += '_' + layer.name
else:
if layer.parent.name == root.name:
layer.visible = True
return name
def check_duplicate_parent(combination):
parent_set = set()
for layer_img in combination:
parent_set.add(layer_img.parent)
return len(parent_set) < len(combination)
def export_img(list_groups, grant):
list_img = []
for group in list_groups:
group.visible = True
if group.name.lower() == 'background':
continue
for layer_img in group:
if layer_img.kind != 'group' and layer_img.name.lower() != 'background':
layer_img.visible = False
list_img.append(layer_img)
elif layer_img.name.lower() == 'background':
layer_img.visible = True
else:
layer_img.visible = False
list_img = itertools.permutations(list_img, r=len(list_groups))
for index, combination in enumerate(list_img):
if not check_duplicate_parent(combination):
name = set_visible(combination, True, grant)
if not os.path.isfile(f'./lab1/{name}.png'):
image = grant.composite(ignore_preview=True)
image.save(f'./lab1/{name}.png')
set_visible(combination, False, grant)
yield {
"name": name,
"url": f'./lab1/{name}.png'
}
def get_object(root, layers, background):
result = []
for layer in layers:
if layer.is_group():
layer.visible = True
if layer.name.lower() == background:
obj = {
"name": layer.name,
"url": root.composite().save(f"{layer.name}.png")
}
result.append(obj)
continue
else:
obj = {
f"{layer.name}": get_object(root, layer, background)
}
result.append(obj)
else:
list_parent = []
layer_parent = layer.parent
if layer.parent.name == root.name:
continue
list_parent = find_parent(layer_parent)
if len(list_parent) > 0:
for parent in list_parent:
if parent.name != layer.parent.name:
parent.visible = False
if list_parent in list_layer_parent:
if len(list_parent) > 0:
continue
else:
result.append({
"name": f"{layer.name}",
"url": f"./lab1/{layer.name}.png"
})
layer.visible = True
if not os.path.isfile(f'./lab1/{layer.name}.png'):
image = root.composite(ignore_preview=True)
image.save(f'./lab1/{layer.name}.png')
continue
list_layer_parent.append(list_parent)
list_combination = []
if len(list_parent) > 1 and list_parent[0].parent.name != root.name:
for item in export_img(list_parent, root):
list_combination.append(item)
result.append({
"name": f"{layer.name}",
"url": f"./lab1/{layer.name}.png",
"combination": list_combination
})
elif len(list_parent) == 1:
grant = list_parent[0].parent
if not grant.parent or grant.name == root.name:
for index, item in enumerate(grant.parent):
if item.name.lower() == background or item.name == grant.name:
grant.parent[index].visible = True
else:
grant.parent[index].visible = False
set_visible(layer_parent, False, root)
for item in layer_parent:
item.visible = True
name = item.name
result.append({
"name": f"{layer.name}",
"url": f"./lab1/{layer.name}.png"
})
if not os.path.isfile(f'./lab1/{name}.png'):
image = root.composite(ignore_preview=True)
image.save(f'./lab1/{name}.png')
item.visible = False
set_visible(layer_parent.parent, False, root)
psd = _Const(PSDImage.open('/home/anhmeo/Desktop/Once upon a time there was a girl who loves dogs. The end..psd'))
get_object(psd.psd, psd.psd, 'background')