-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheightmapsplit.py
More file actions
83 lines (70 loc) · 2.09 KB
/
Copy pathheightmapsplit.py
File metadata and controls
83 lines (70 loc) · 2.09 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
import sys
import getopt
from PIL import Image
import numpy as np
Image.MAX_IMAGE_PIXELS = 9999999999
def usage():
print('heightmapsplit.py -i <input file> -w <split width> -h <split height> -x <x offset> -y <y offset> -p <output file prefix>')
print('\nNote: assume image origin as top-left of the image')
# old API key is dmlO1fVQRPKI-GrVIYJ1YA
# my API key is hUYXm28xTuq1x7qLoLuKwA
def main(argv):
try:
opts, args = getopt.getopt(argv,"i:w:h:x:y:p:",[])
except getopt.GetoptError:
usage()
sys.exit(2)
myInput = ""
myWidth = ""
myHeight = ""
myXOfffset = ""
myYOffset = ""
myPrefix = ""
for opt, arg in opts:
if opt == "-i":
myInput = arg
elif opt == "-w":
myWidth = arg
elif opt == "-h":
myHeight = arg
elif opt == "-x":
myXOfffset = arg
elif opt == "-y":
myYOffset = arg
elif opt == "-p":
myPrefix = arg
if ((myInput == '') or (myWidth == '') or (myHeight == '') or (myXOfffset == '') or (myYOffset == '') or (myPrefix == '')):
usage()
sys.exit(2)
inputFile = myInput
width = int(myWidth)
height = int(myHeight)
xOfffset = int(myXOfffset)
yOffset = int(myYOffset)
prefix = myPrefix
print("Loading image...")
img = Image.open(inputFile)
imgArr = np.asarray(img)
imgWidth = imgArr.shape[1]
imgHeigth = imgArr.shape[0]
x = xOfffset
xCount = 0
while x < imgWidth:
if (x + width > imgWidth):
break
y = yOffset
yCount = 0
while y < imgHeigth:
if (y + height > imgHeigth):
break
outFile = prefix + "_X" +str(xCount) + "_Y" + str(yCount) + ".png"
print("Creating sub-image " + outFile)
outarr = imgArr[y:y+height, x:x+width]
image = Image.fromarray(outarr)
image.save(outFile)
y += height-1
yCount += 1
x += width-1
xCount += 1
if __name__ == "__main__":
main(sys.argv[1:])