-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatchify_images.py
More file actions
55 lines (49 loc) · 1.67 KB
/
Copy pathpatchify_images.py
File metadata and controls
55 lines (49 loc) · 1.67 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
import os
from PIL import Image
def crop_directory(dirs):
for directory in dirs:
a = os.path.join(directory,"input")
b = os.path.join(directory,"GT")
inputImgs = os.listdir(a)
gtImgs = os.listdir(b)
for img in inputImgs:
path = os.path.join(a,img)
crop(directory,path,128,128, img, "input_patches")
for img in gtImgs:
path = os.path.join(b,img)
crop(directory,path,128,128, img, "GT_patches")
def scanSetFolder(folder):
names = os.listdir(folder)
names.sort()
dirs = [os.path.join(folder, name) for name in names]
return dirs, names
def crop(path, input, height, width, imgName, patchName):
k = 0
newDir = os.path.join(path, patchName)
if not os.path.exists(newDir):
os.makedirs(newDir)
try:
im = Image.open(input)
except:
return
imgwidth, imgheight = im.size
for i in range(0,imgheight,height):
for j in range(0,imgwidth,width):
box = (j, i, j+width, i+height)
a = im.crop(box)
a.save(os.path.join(newDir, imgName[:-4] + "_patch_%s.jpg" % k))
k +=1
if __name__ == "__main__":
trainDir = os.path.join(os.path.dirname(os.path.abspath(__file__)),"training-data")
testDir = os.path.join(os.path.dirname(os.path.abspath(__file__)),"testing-data")
validDir = os.path.join(os.path.dirname(os.path.abspath(__file__)),"validation-data")
trainDirs, trainNames = scanSetFolder(trainDir)
testDirs, testNames = scanSetFolder(testDir)
validDirs, validNames = scanSetFolder(validDir)
print("croopping training data...")
crop_directory(trainDirs)
print("croopping testing data...")
crop_directory(testDirs)
print("croopping validation data...")
crop_directory(validDirs)
print("done!")