-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.py
More file actions
299 lines (247 loc) · 11.8 KB
/
Util.py
File metadata and controls
299 lines (247 loc) · 11.8 KB
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
import os
import json
import random
from typing import List, Tuple
from datetime import datetime
import numpy as np
import cv2
import random
class Util:
def allDirs(self, rootdir: str, localeBlacks: list) -> list:
ret = []
for dir in os.listdir(rootdir):
if any(black in dir for black in localeBlacks):
continue
d = os.path.join(rootdir, dir)
if os.path.isdir(d):
ret.append(d)
ret += self.allDirs(d, localeBlacks)
return ret
def allFilesTwoList(self, rootdir: str, localeBlacks: list, ext: str) -> Tuple[List[str], List[str]]:
allDirsRet = self.allDirs(rootdir, localeBlacks)
allDirsRet.append(rootdir)
ret0 = []
ret1 = []
#------------------------------------------------------------------------------------------
for dir in allDirsRet:
dir2fileName_list = os.listdir(dir)
for dir2fileName in dir2fileName_list:
# ext가 비어있으면 모든 파일을 수집, 아니면 확장자를 체크
if not ext or dir2fileName.lower().endswith(ext):
fullName = os.path.join(dir, dir2fileName)
ret0.append(fullName)
ret1.append(fullName)
return ret0, ret1
def allFilesSet(self, rootdir: str, localeBlacks: list, ext: str):
allDirsRet = self.allDirs(rootdir, localeBlacks)
allDirsRet.append(rootdir)
ret = set()
#------------------------------------------------------------------------------------------
for dir in allDirsRet:
dir2fileName_list = os.listdir(dir)
for dir2fileName in dir2fileName_list:
# ext가 비어있으면 모든 파일을 수집, 아니면 확장자를 체크
if not ext or dir2fileName.lower().endswith(ext):
fullName = os.path.join(dir, dir2fileName)
ret.add((fullName, os.path.getsize(fullName)))
return ret
def procTime(self, start_time) -> str:
now = datetime.now()
elapsed_time = now - start_time
total_seconds = elapsed_time.total_seconds()
formatted_time = f"{total_seconds:.2f}" # 소수점 둘째 자리까지 표시
return f"{formatted_time}"
def checkMoreThanSec(self, start_time, sec: int) -> bool:
now = datetime.now()
elapsed_seconds = (now - start_time).total_seconds()
if elapsed_seconds > sec:
return True
else:
return False
def pickImageLocale(self, localeInp: str, localeBlacks: list, dropD: int, dropS: int, pick_count: int, ext: str):
ret = []
pickAll = []
#------------------------------------------------------------------------------------------
allDirsRet = self.allDirs(localeInp, localeBlacks)
allDirsRet.append(localeInp)
for dir in allDirsRet:
dir2fileName_list = os.listdir(dir)
for dir2fileName in dir2fileName_list:
# ext가 비어있으면 모든 파일을 수집, 아니면 확장자를 체크
if not ext or dir2fileName.lower().endswith(ext):
fullName = os.path.join(dir, dir2fileName)
pickAll.append(fullName)
#------------------------------------------------------------------------------------------
try:
with open('./dropcache.json', 'r') as f:
dropDict = json.load(f)
except FileNotFoundError:
dropDict = dict()
#------------------------------------------------------------------------------------------
# 예외 처리: 사용할 수 있는 파일이 너무 적을 경우
#if (len(pickAll) + pick_count) < len(dropDict):
# print("!!! : Small Result, Please edit Distance or Step")
# dropDict = dict()
# return random.sample(pickAll, min(len(pickAll), pick_count))
#------------------------------------------------------------------------------------------
pickEdit_dropCache = [i for i in pickAll if i not in dropDict]
#------------------------------------------------------------------------------------------
for i in range(pick_count):
if not pickEdit_dropCache:
break # 남은 파일이 없으면 중단
pick_value = random.choice(pickEdit_dropCache)
ret.append(pick_value)
pickEdit_dropCache.remove(pick_value)
dropDict[pick_value] = dropS
#---------------------------------------------------
real_idx = pickAll.index(pick_value)
# 삭제할 항목을 미리 저장
to_remove = set()
for j in range(real_idx, max(0, real_idx - dropD - 1), -1):
if pickAll[j] not in dropDict:
to_remove.add(pickAll[j])
dropDict[pickAll[j]] = dropS
for j in range(real_idx, min(len(pickAll), real_idx + dropD + 1)):
if pickAll[j] not in dropDict:
to_remove.add(pickAll[j])
dropDict[pickAll[j]] = dropS
pickEdit_dropCache = [x for x in pickEdit_dropCache if x not in to_remove]
#------------------------------------------------------------------------------------------
dropDict = {key: value - 1 for key, value in dropDict.items() if value > 1}
with open('dropcache.json', 'w') as f:
json.dump(dropDict, f, indent=4)
return ret
def resizeAndPutText(self, file_abs_path: str, tagOn: bool, dateType: int, localeTags: dict, namePattern, w=1920, h=1080, tx=1528, ty=1040):
size = (w, h)
file_name = os.path.basename(file_abs_path)
base_pic = np.zeros((size[1], size[0], 3), np.uint8)
pic1 = cv2.imread(file_abs_path, cv2.IMREAD_COLOR)
try:
h, w = pic1.shape[:2]
except:
print("치명적인 문제!")
print(file_abs_path)
return
ash = size[1] / h
asw = size[0] / w
if asw < ash:
sizeas = (int(w * asw), int(h * asw))
else:
sizeas = (int(w * ash), int(h * ash))
pic1 = cv2.resize(pic1, dsize=sizeas)
base_pic[int(size[1] / 2 - sizeas[1] / 2):int(size[1] / 2 + sizeas[1] / 2),
int(size[0] / 2 - sizeas[0] / 2):int(size[0] / 2 + sizeas[0] / 2), :] = pic1
if tagOn:
if dateType == 0:
tag = os.path.getctime(file_abs_path)
timetag = datetime.fromtimestamp(tag).strftime('%Y.%m.%d %H:%M')
elif dateType == 1:
tag = os.path.getmtime(file_abs_path)
timetag = datetime.fromtimestamp(tag).strftime('%Y.%m.%d %H:%M')
elif dateType == 2:
search_res = namePattern.search(file_name)
try:
search_res = search_res.groups()
timetag = '%s.%s.%s %s:%s' % (search_res[0], search_res[1], search_res[2], search_res[3], search_res[4])
except:
tag = os.path.getmtime(file_abs_path)
timetag = datetime.fromtimestamp(tag).strftime('%Y.%m.%d %H:%M')
else:
return
untagch = True
for key in localeTags.keys():
if key in file_abs_path:
timetag += f" {localeTags[key]}"
untagch = False
break
if untagch:
if "__ELSE__" in localeTags.keys():
timetag += f' {localeTags["__ELSE__"]}'
else:
timetag += " __"
cv2.putText(base_pic, timetag, (tx, ty), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 1, (0, 0, 0), 4, cv2.LINE_AA)
cv2.putText(base_pic, timetag, (tx, ty), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 1, (255, 255, 255), 1, cv2.LINE_AA)
cv2.imwrite('./' + file_name, base_pic)
def resizeAndPutTextJD(self, file_abs_path: str, tagOn, dateType, localeTags: dict, namePattern, w=1920, h=1080, splitSize=2, textSize=1, tx=650, ty=515):
size = (w // splitSize, h // splitSize)
file_name = os.path.basename(file_abs_path)
base_pic = np.zeros((size[1], size[0], 3), np.uint8)
pic1 = cv2.imread(file_abs_path, cv2.IMREAD_COLOR)
try:
h, w = pic1.shape[:2]
except:
print("치명적인 문제!")
print(file_abs_path)
return
ash = size[1] / h
asw = size[0] / w
if asw < ash:
sizeas = (int(w * asw), int(h * asw))
else:
sizeas = (int(w * ash), int(h * ash))
pic1 = cv2.resize(pic1, dsize=sizeas)
base_pic[int(size[1] / 2 - sizeas[1] / 2):int(size[1] / 2 + sizeas[1] / 2),
int(size[0] / 2 - sizeas[0] / 2):int(size[0] / 2 + sizeas[0] / 2), :] = pic1
if tagOn:
if dateType == 0:
tag = os.path.getctime(file_abs_path)
timetag = datetime.fromtimestamp(tag).strftime('%Y.%m.%d %H:%M')
elif dateType == 1:
tag = os.path.getmtime(file_abs_path)
timetag = datetime.fromtimestamp(tag).strftime('%Y.%m.%d %H:%M')
elif dateType == 2:
search_res = namePattern.search(file_name)
try:
search_res = search_res.groups()
timetag = '%s.%s.%s %s:%s' % (search_res[0], search_res[1], search_res[2], search_res[3], search_res[4])
except:
tag = os.path.getmtime(file_abs_path)
timetag = datetime.fromtimestamp(tag).strftime('%Y.%m.%d %H:%M')
else:
return
untagch = True
for key in localeTags.keys():
if key in file_abs_path:
timetag += f" {localeTags[key]}"
untagch = False
break
if untagch:
if "__ELSE__" in localeTags.keys():
timetag += f' {localeTags["__ELSE__"]}'
else:
timetag += " __"
cv2.putText(base_pic, timetag, (tx, ty), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, textSize, (0, 0, 0), 4, cv2.LINE_AA)
cv2.putText(base_pic, timetag, (tx, ty), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, textSize, (255, 255, 255), 1, cv2.LINE_AA)
cv2.imwrite('./' + file_name, base_pic)
"""
def merge(file_list, w=1920, h=1080, splitSize=2):
w = w // splitSize
h = h // splitSize
result_image = np.zeros((h*splitSize, w*splitSize, 3), dtype=np.uint8)
fidx = 0
nidx = 0
retList = []
while fidx != len(file_list):
for i in range(splitSize):
for j in range(splitSize):
pic = cv2.imread(file_list[fidx][0])
fidx += 1
result_image[i*h:(i+1)*h, j*w:(j+1)*w] = pic
cv2.imwrite(f'./{nidx}.png', result_image)
retList.append(f'./{nidx}.png')
nidx += 1
return retList
"""
def merge(self, file_list, w=1920, h=1080, splitSize=2):
w = w // splitSize
h = h // splitSize
result_image = np.zeros((h*splitSize, w*splitSize, 3), dtype=np.uint8)
fidx = 0
while fidx != len(file_list):
for i in range(splitSize):
for j in range(splitSize):
pic = cv2.imread("./"+ os.path.basename(file_list[fidx]))
fidx += 1
result_image[i*h:(i+1)*h, j*w:(j+1)*w] = pic
cv2.imwrite(f'./jd.png', result_image)
return