-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessor.py
More file actions
40 lines (33 loc) · 1.11 KB
/
Copy pathpreprocessor.py
File metadata and controls
40 lines (33 loc) · 1.11 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
import glob
import os
from math import sqrt, ceil
from PIL import Image
import time
save_path = "./img_temp/"
def preprocess(data_path):
fname_list = []
for fname in glob.iglob(data_path + '/*'):
if os.path.isdir(fname):
preprocess(fname)
if not os.path.isfile(fname):
continue
start = time.time()
print(fname)
offext = fname.split('.')[-2].split('/')[-1].split('\\')[-1]
imgname = save_path + offext + '.png'
print(offext)
with open(fname, 'rb') as infile:
fsize = os.stat(fname).st_size
n = ceil(sqrt(fsize))
inf_content = infile.read()
if n**2 > fsize:
for i in range(n**2 - fsize):
inf_content += b'0'
img = Image.frombytes('L', (n, n), bytes(inf_content))
img = img.resize((224, 224))
img.save(imgname)
fname_list.append(imgname)
print(time.time() - start)
def delete():
for fname in glob.iglob(save_path + '/*'):
os.remove(fname)