-
Notifications
You must be signed in to change notification settings - Fork 2
/
normalize.py
executable file
·68 lines (53 loc) · 1.92 KB
/
normalize.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
#!/usr/bin/env python3
#Converts all images in a given folder to the same shape, format and then saves them to another directory
#Usage: ./normalize [directory] [target_dir]
#[directory] - source directory
#[target_dir] - destination directory
import cv2
import os
import sys
from PIL import Image
import numpy as np
from hashlib import md5
import shutil
import tqdm
import random
import string
def main():
if len(sys.argv) != 3:
print("Usage: ./normalize [directory] [target_dir]")
return
try:
source_dir = sys.argv[1]
dest_dir = sys.argv[2]
if not os.path.isdir(source_dir):
raise RuntimeError()
if not os.path.isdir(dest_dir):
os.mkdir(dest_dir)
except:
print("Malformed args")
return
filenames = [f for f in os.listdir(source_dir) if os.path.isfile(os.path.join(source_dir, f))]
tmpfile = '/tmp/tmp' + ''.join(random.choices(string.ascii_uppercase + string.digits, k=8)) + '.png'
for filename in tqdm.tqdm(filenames):
path_to_file = os.path.join(source_dir, filename)
rawdata = np.array(Image.open(path_to_file).convert("RGB"))
rawdata = cv2.cvtColor(rawdata, cv2.COLOR_RGB2BGR)
height = rawdata.shape[1]
width = rawdata.shape[0]
factor_h = 256/height
factor_w = 256/width
factor = max(factor_h, factor_w)
rawdata = cv2.resize(rawdata, (round(height*factor), round(width*factor)))
height = rawdata.shape[1]
width = rawdata.shape[0]
posh = int(float(height)/2-256/2)
posw = int(float(width)/2-256/2)
cv2.imwrite(tmpfile, rawdata[posw:(posw+256), posh:(posh+256), :])
hash = md5(open(tmpfile, 'rb').read()).hexdigest()
dest_path = os.path.join(dest_dir, hash+".png")
shutil.move(tmpfile, dest_path)
#print("%s\t=>\t%s"%(path_to_file,dest_path))
return
if __name__=="__main__":
main()