-
Notifications
You must be signed in to change notification settings - Fork 0
/
Civ3Tileset.py
284 lines (263 loc) · 11.2 KB
/
Civ3Tileset.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import requests
import os
import shutil
import numpy as np
from PIL import Image, ImageSequence
from PIL.Image import Palette
from guizero import App, Text, PushButton, Window, TextBox
app = App(title="C3TS Civ 3 Tileset")
space = Text(app, text="")
text1 = Text(app, text="Choose C3TS Input File")
space1 = Text(app, text="")
textbox1 = TextBox(app, text="Civ 3 Standard.c3ts")
def make_spec(tag, folder):
spec = open(folder+tag+"/"+tag+".spec","w+")
spec.write('''
;Made by Civ III Tileset for freeciv
;All graphics remain the property of their creators and are subject to their licenses
[spec]
; Format and options of this spec file:
options = "+Freeciv-3.0-spec"
[info]
;
artists = "
Civ III Graphics Community
"
[extra]
sprites =
{ "tag", "file"\n''')
spec.write(' "u.'+tag[2:len(tag)]+'_s", "Civ3Tileset v3.0/'+folder+tag+'/final_'+tag+'_s"\n')
spec.write(' "u.'+tag[2:len(tag)]+'_se", "Civ3Tileset v3.0/'+folder+tag+'/final_'+tag+'_se"\n')
spec.write(' "u.'+tag[2:len(tag)]+'_e", "Civ3Tileset v3.0/'+folder+tag+'/final_'+tag+'_e"\n')
spec.write(' "u.'+tag[2:len(tag)]+'_ne", "Civ3Tileset v3.0/'+folder+tag+'/final_'+tag+'_ne"\n')
spec.write(' "u.'+tag[2:len(tag)]+'_n", "Civ3Tileset v3.0/'+folder+tag+'/final_'+tag+'_n"\n')
spec.write(' "u.'+tag[2:len(tag)]+'_nw", "Civ3Tileset v3.0/'+folder+tag+'/final_'+tag+'_nw"\n')
spec.write(' "u.'+tag[2:len(tag)]+'_w", "Civ3Tileset v3.0/'+folder+tag+'/final_'+tag+'_w"\n')
spec.write(' "u.'+tag[2:len(tag)]+'_sw", "Civ3Tileset v3.0/'+folder+tag+'/final_'+tag+'_sw"\n')
spec.write(''' }
*include override/flags.spec''')
spec.close()
def convert_img_bg(pic):
palette = np.array(pic.getpalette(), dtype=np.uint8).reshape((256,3))
alpha = [
[25, 0, 25],
[40,0,40],
[56,0,56],
[70,0,70],
[100,0,100],
[100,0,100],
[100,0,100],
[100,0,100],
[100,0,100],
[120,0,120],
[140,0,140],
[160,0,160],
[180,0,180],
[200,0,200],
[220,0,220],
[255,0,255],
[255,0,255],
[255,0,255],
[255,0,255]
]
new_palette = []
a=0
i=0
for colour in palette:
i += 1
if i < 239:
new_palette.append(colour[0])
new_palette.append(colour[1])
new_palette.append(colour[2])
#new_palette.append(255)
else:
new_palette.append(alpha[a][0])
new_palette.append(alpha[a][1])
new_palette.append(alpha[a][2])
#new_palette.append(alpha[a][3])
a+=1
pic.putpalette(new_palette, rawmode="RGB")
return pic
def convert_to_transparent(path):
pic = Image.open(path)
pic = pic.quantize(colors=128)
palette = np.array(pic.getpalette(), dtype=np.uint8).reshape((256,3))
#print(palette)
new_palette = []
#print(pic.getpixel((0,0)))
for colour in palette:
# if colour.all() == palette[pic.getpixel((0,0))].all():
# new_palette.append(0)
# new_palette.append(0)
# new_palette.append(0)
# new_palette.append(0)
# #print(colour)
# #print(palette[pic.getpixel((0,0))])
if colour[0] == colour[2]+1 or colour[0] == colour[2] and colour[1] <= colour[0]/2:
new_palette.append(colour[0])
new_palette.append(colour[0])
new_palette.append(colour[0])
new_palette.append(255 - colour[0])
else:
new_palette.append(colour[0])
new_palette.append(colour[1])
new_palette.append(colour[2])
new_palette.append(255)
pic.putpalette(new_palette, rawmode="RGBA")
pic.save(path)
def crop_to_cimpletoon(path, name, outputpath):
minx = 0
miny = 0
maxx = 0
maxy = 0
imgs = {}
imgs["sw"] = Image.open(path+"/d_sw.png")
imgs["s"] = Image.open(path+"/d_s.png")
imgs["se"] = Image.open(path+"/d_se.png")
imgs["e"] = Image.open(path+"/d_e.png")
imgs["ne"] = Image.open(path+"/d_ne.png")
imgs["n"] = Image.open(path+"/d_n.png")
imgs["nw"] = Image.open(path+"/d_nw.png")
imgs["w"] = Image.open(path+"/d_w.png")
for img in imgs:
#print("next pic")
zero = imgs[img].getpixel((0,0))
for x in range(imgs[img].width):
for y in range(imgs[img].height):
if imgs[img].getpixel((x,y)) == zero:
pass
else:
if x > maxx:
maxx = x
if x < minx or minx == 0:
minx = x
if y > maxy:
maxy = y
if y < miny or miny == 0:
miny = y
print([minx, maxx, miny, maxy])
print("\n")
if (maxx-minx)/64 > (maxy-miny)/48:
yoffset = (48*((maxx-minx)/64)-(maxy-miny))/2
for img in imgs:
to_be_saved = imgs[img].crop((minx, miny-yoffset, maxx, maxy+yoffset)).copy()
to_be_saved.save(path+"/cropped_"+img+".png")
to_be_saved.resize((64,48), resample=Image.LANCZOS).save((outputpath+name+"/final_"+name+"_"+img+".png"))
if (maxx-minx)/64 < (maxy-miny)/48:
xoffset = (64*((maxy-miny)/48)-(maxx-minx))/2
for img in imgs:
to_be_saved = imgs[img].crop((minx-xoffset, miny, maxx+xoffset, maxy)).copy()
to_be_saved.save(path+"/cropped_"+img+".png")
to_be_saved.resize((64,48), resample=Image.LANCZOS).save((outputpath+name+"/final_"+name+"_"+img+".png"))
make_spec(name, outputpath)
def convert():
units=open(textbox1.value, "r")
line_num=0
finalPath="output/tileset/"
tags=[]
urls=[]
for line in units:
if line[0] == "h":
urls.append(line)
elif line[0] == "u":
tags.append(line)
elif line[0] == "P":
finalPath = line[1:len(line)].strip()
elif line[0] == "C":
finalPath = line.strip()
else:
pass
for link in urls:
tag = tags[urls.index(link)].strip()
cwd = os.getcwd()
os.makedirs(cwd+"/output/tileset/"+tag)
os.makedirs(cwd+"/"+finalPath+tag)
if os.path.exists(tag+".rar") == False:
file = requests.get(link)
open(tag+".rar", "wb").write(file.content)
os.system('cmd /c "7z.exe e "'+tag+'".rar -ooutput/temp/"'+tag+'" -y"')
for file in os.listdir("output/temp/"+tag):
if file.endswith('.flc') and file.count("Strafe") == 0 and file.count("VTOL") == 0 and os.path.exists("output/tileset/"+tag+"/d_sw.png") == False:
temp_img = Image.open("output/temp/"+tag+"/"+file)
try:
temp_img.save("output/temp/"+tag+"/test.gif", save_all=True, loop=0)
except OSError:
print("Couldn't Save GIF File")
else:
# try:
# test_img = Image.open("output/temp/"+tag+"/Default.flc")
# test_img.save("output/temp/"+tag+"/test.gif", save_all=True, loop=0)
# except FileNotFoundError:
# print("Loading prefered .flc failed. Using Fallback")
# except OSError:
# print("Loading prefered .flc failed. Using Fallback")
# else:
# temp_img=test_img
convert_img_bg(temp_img).save("output/temp/"+tag+"/test.gif", save_all=True, loop=0)
img = temp_img.copy()
gif = Image.open("output/temp/"+tag+"/test.gif")
print(gif.is_animated)
print("frames: "+str(gif.n_frames))
frame_loc = 0
for frame in ImageSequence.Iterator(gif):
frame_loc += 1
if frame_loc == 1:
try:
frame.save("output/tileset/"+tag+"/d_s.png")
except OSError:
#End of sequence
print("Couldn't Save PNG File")
elif frame_loc == (gif.n_frames/8)+2:
try:
frame.save("output/tileset/"+tag+"/d_se.png")
except OSError:
#End of sequence
print("Couldn't Save PNG File")
elif frame_loc == (gif.n_frames/4)+3:
try:
frame.save("output/tileset/"+tag+"/d_e.png")
except OSError:
#End of sequence
print("Couldn't Save PNG File")
elif frame_loc == ((gif.n_frames/8)*3)+4:
try:
frame.save("output/tileset/"+tag+"/d_ne.png")
except OSError:
#End of sequence
print("Couldn't Save PNG File")
elif frame_loc == ((gif.n_frames/8)*4)+5:
try:
frame.save("output/tileset/"+tag+"/d_n.png")
except OSError:
#End of sequence
print("Couldn't Save PNG File")
elif frame_loc == ((gif.n_frames/8)*5)+6:
try:
frame.save("output/tileset/"+tag+"/d_nw.png")
except OSError:
#End of sequence
print("Couldn't Save PNG File")
elif frame_loc == ((gif.n_frames/8)*6)+7:
try:
frame.save("output/tileset/"+tag+"/d_w.png")
except OSError:
#End of sequence
print("Couldn't Save PNG File")
elif frame_loc == ((gif.n_frames/8)*7)+8:
try:
frame.save("output/tileset/"+tag+"/d_sw.png")
except OSError:
#End of sequence
print("Couldn't Save PNG File")
convert_to_transparent("output/tileset/"+tag+"/d_s.png")
convert_to_transparent("output/tileset/"+tag+"/d_se.png")
convert_to_transparent("output/tileset/"+tag+"/d_e.png")
convert_to_transparent("output/tileset/"+tag+"/d_ne.png")
convert_to_transparent("output/tileset/"+tag+"/d_n.png")
convert_to_transparent("output/tileset/"+tag+"/d_nw.png")
convert_to_transparent("output/tileset/"+tag+"/d_w.png")
convert_to_transparent("output/tileset/"+tag+"/d_sw.png")
crop_to_cimpletoon("output/tileset/"+tag, tag, finalPath)
space2 = Text(app, text="")
button1 = PushButton(app, text="Convert", command=convert)
app.display()