forked from skaparelos/openPacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenPacker.py
186 lines (137 loc) · 4.05 KB
/
openPacker.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import sys
import json
from PIL import Image
import random
class ImageData():
def __init__(self, name, im, w, h):
self._name = name
self._image = im
self._width = w
self._height = h
self._area = w*h
def setX(self, x):
self._x = x
def setY(self, y):
self._y = y
def getX(self):
return self._x
def getY(self):
return self._y
def getName(self):
return self._name
def getImage(self):
return self._image
def getWidth(self):
return self._width
def getHeight(self):
return self._height
def getArea(self):
return self._area
class Point():
def __init__ (self, x, y, valid=True):
self.x = x
self.y = y
self. valid = valid
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __hash__(self):
return hash((self.x, self.y))
def __str__(self):
return "("+str(self.x)+","+str(self.y)+") " + ("valid" if self.valid else "invalid")
def loadImages(names):
images = []
totalWidth = 0
maxHeight = 0
for name in names:
im = Image.open(name)
images.append(ImageData(name, im, im.width, im.height))
totalWidth += im.width
maxHeight = max(maxHeight, im.height)
return (images, totalWidth, maxHeight)
def taken(x,y,w,h, images):
for im in images:
if (x in range(im.getX(), im.getX() + im.getWidth()) or im.getX() in range( x, x + w )) and (y in range(im.getY(), im.getY() + im.getHeight()) or im.getY() in range(y, y + h)):
return True
return False
def generateAtlas(imageData):
images = imageData[0]
images.sort(key=lambda x: x.getHeight() * x.getWidth(), reverse=True)
images[0].setX(0)
images[0].setY(0)
totalWidth = imageData[1]
maxHeight = images[0].getHeight()
x_offset = images[0].getWidth()
y_offset = 0
curWidth = 0
curHeight = maxHeight
pointsToCheck = []
placedImages = []
for im in images[1:]:
placed = False
for p in pointsToCheck:
x = p.x
y = p.y
stillValid = p.valid
# the space can and sometimes will be taken due a variety of
# different sized objects being next to each other,
# so check if we can fit in the spcae and move on if so.
if taken( x, y, im.getWidth(), im.getHeight(), placedImages):
continue
if (stillValid and (y + im.getHeight() < curHeight)):
im.setX(x)
im.setY(y)
p.valid = False
placed = True
curWidth = max(curWidth, im.getX() + im.getWidth())
newPoint = Point(im.getX(), im.getY() + im.getHeight())
if ( not(newPoint in pointsToCheck) ):
pointsToCheck.append(newPoint)
break # break is important as the exactly above line adds more points so we get into an infinite loop
elif (stillValid and (x + im.getWidth() < curWidth)):
im.setX(x)
im.setY(y)
p.valid = False
placed = True
curHeight = max(curHeight, im.getY() + im.getHeight())
newPoint = Point(im.getX() + im.getWidth(), im.getY())
if ( not(newPoint in pointsToCheck) ):
pointsToCheck.append(newPoint)
break # break is important as the exactly above line adds more points so we get into an infinite loop
if placed == False:
im.setX(x_offset)
im.setY(y_offset)
newPoint = Point(im.getX(), im.getY() + im.getHeight())
if (not (newPoint in pointsToCheck)):
pointsToCheck.append(newPoint)
x_offset += im.getWidth()
curWidth += im.getWidth()
placedImages.append(im)
newImage = Image.new('RGBA', (curWidth, curHeight))
for im in images:
newImage.paste(im.getImage(), (im.getX(), im.getY()))
return newImage
def export(images, newImage):
# write the image
newImage.save('spritesheet.png')
# make JSON
framesJson = []
for im in images[0]:
f = {}
f["filename"] = im.getName()
f["frame"] = {}
f["frame"]["x"] = im.getX()
f["frame"]["y"] = im.getY()
f["frame"]["w"] = im.getWidth()
f["frame"]["h"] = im.getHeight()
framesJson.append(f)
f = open("out.json", 'w')
f.write(json.dumps(framesJson, indent = 4))
f.close()
def main():
names = sys.argv[1:]
#names = ['iso_imgs/cinema.png', 'iso_imgs/green.png', 'iso_imgs/tilenew.png']
images = loadImages(names)
newImage = generateAtlas(images)
export(images, newImage)
if __name__ == '__main__':
main()