-
Notifications
You must be signed in to change notification settings - Fork 3
/
sprites_utils.py
executable file
·96 lines (85 loc) · 3.18 KB
/
sprites_utils.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
#!/bin/python3
from PIL import Image
from os import listdir, path, makedirs
import re
def addTransparentBackground(fullpath, newPath):
if not path.exists(fullpath):
print('couldn\'t find ' + fullpath)
return
img = Image.open(fullpath)
#sometimes you need to do that because the front is mixed with front anim
img = img.crop((0, 0, 64, 64))
# add a transparency layer
img = img.convert("RGBA")
datas = img.getdata()
newData = []
# the first pixel is considered the transparency color
trns = datas[0]
for item in datas:
if item == trns:
# replace by a transparent pixel
newData.append((255, 255, 255, 0))
else:
newData.append(item)
img.putdata(newData)
img.save(newPath)
def getPaletteColors(line):
colors = line.split(' ')
red = int(colors[0])
green = int(colors[1])
blue = int(colors[2])
return (red, green, blue)
def getPaletteList(filepath):
if not path.exists(filepath):
return
with open(filepath, 'r') as palFile:
lNumber = 0
palList = []
for line in palFile:
lNumber += 1
# line 3 has the number of colors in the palette, here 16
if lNumber <= 3:
continue
for c in line.strip("\n").split(' '):
palList.append(int(c))
return palList
def applyPalette(inputImagePath, inputPaletteFile, outputImagePath):
img = Image.open(inputImagePath)
img.putpalette(getPaletteList(inputPaletteFile))
img.save(outputImagePath, format="png")
# the .png isn't accurate, sometimes the palette differs
def getNormal(paletteFolder, ImageFolder, imageName):
normalPalPath = paletteFolder + imageName + ".pal"
inputImagePath = ImageFolder + imageName + ".png"
if not path.exists(inputImagePath) or not path.exists(normalPalPath):
print('One file doesn\'t exist:', inputImagePath, normalPalPath)
return
outputImagePath = ImageFolder + imageName + ".png"
applyPalette(inputImagePath, normalPalPath, outputImagePath)
def getShiny(paletteFolder, ImageFolder, imageName):
shinyPalPath = paletteFolder + "shiny_" + imageName + ".pal"
inputImagePath = ImageFolder + imageName + ".png"
if not path.exists(inputImagePath) or not path.exists(shinyPalPath):
print('One file doesn\'t exist:', inputImagePath, shinyPalPath)
return
outputImagePath = ImageFolder + "SHINY_" + imageName + ".png"
applyPalette(inputImagePath, shinyPalPath, outputImagePath)
return
# todo fix this one
#getShiny("./out/palettes/", "./out/sprites/", "PANGORO")
files = listdir("./out/sprites/")
shinyRegex = re.compile('SHINY_')
for name in files:
# do not shiny the shynies
if shinyRegex.match(name):
continue
name = name.replace('.png', '')
try:
getShiny("./out/palettes/", "./out/sprites/", name)
getNormal("./out/palettes/", "./out/sprites/", name)
except Exception as e:
print("Couldn't get shiny of " + name + ", reason: " + str(e))
pass
files = listdir("./out/sprites/")
for name in files:
addTransparentBackground("./out/sprites/" + name, "./static/sprites/" + name)