forked from TeamLumi/opendpr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombine.py
More file actions
118 lines (85 loc) · 2.84 KB
/
combine.py
File metadata and controls
118 lines (85 loc) · 2.84 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
from PIL import Image
import numpy as np
from glob import glob
import time
from os import path, remove
import rapidjson as json
from tqdm import tqdm
import re
import asyncio
async def getfps(extensions):
fps = []
for ext in extensions:
fps += glob('Assets/**/**.' + ext, recursive=True)
return fps
class reSub:
def __init__(self, dict):
self.dict = dict
self.regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
async def replace(self, text):
return self.regex.subn(lambda mo: self.dict[mo.string[mo.start():mo.end()]], text)
async def getGuid(fp):
metaPath = fp + '.meta'
with open(metaPath, 'r') as f:
guid = f.read().split('\n')[1].split(':')[1].strip()
f.close()
return guid
async def getImage(fp):
img = Image.open(fp)
img = np.array(img)
imgHash = hash(img.data.tobytes())
return img, str(imgHash)
async def getImageReplacements():
uniqueImages = {}
guidSubs = {}
deleteImages = []
imageSet = set()
i = 0
fps = await getfps(['png'])
for fp in tqdm(fps, total=len(fps)):
img, imgHash = await getImage(fp)
guid = await getGuid(fp)
imageSet.add(imgHash)
if imgHash not in uniqueImages:
uniqueImages[imgHash] = {'path': fp, 'guid': guid, 'guids': [guid]}
else:
guidSubs[guid] = uniqueImages[imgHash]['guid']
uniqueImages[imgHash]['guids'].append(guid)
deleteImages.append(fp)
return guidSubs, deleteImages
async def overwriteGUIDs(fp, reSubs):
with open(fp, 'r') as f:
data = f.read()
f.close()
replaced, count = await reSubs.replace(data)
if count > 0:
with open(fp, 'w') as f:
f.write(replaced)
return count
async def replaceGUIDs(guidSubs):
fps = await getfps(['mat'])
reSubs = reSub(guidSubs)
proceses = []
for fp in tqdm(fps):
proceses.append(overwriteGUIDs(fp, reSubs))
responses = [await p for p in tqdm(asyncio.as_completed(proceses), total=len(proceses))]
replacements = sum(responses)
return replacements
async def deleteImages(deleteImages):
for fp in tqdm(deleteImages):
metaPath = fp + '.meta'
if path.exists(fp):
remove(fp)
if path.exists(metaPath):
remove(metaPath)
async def main():
start = time.time()
print('Getting image replacements...')
guidSubs, deleteFps = await getImageReplacements()
print('Replacing GUIDs...')
replacements = await replaceGUIDs(guidSubs)
print(f'Replaced {replacements} GUIDs')
print('Deleting unneeded images and metadata...')
await deleteImages(deleteFps)
if __name__ == '__main__':
asyncio.run(main())