| | """Make Sprites
Takes a directory of .TGAs, creates a single 8BPP palette, and then applies that
palette to all of the .TGA files.
Edits the palette so that the transparency color is at index 0.
"""
import os
TEMP_TGA_NAME = "_ALL_TEMP.TGA"
TGA_EXT = ".TGA"
IRFAN_PATH = r"wine ~/.wine/drive_c/Program\ Files\ \(x86\)/IrfanView/i_view32.exe"
PALETTE_NAME = "PALETTE.PAL"
TRANSPARENCY_COLOR = "97 17 163"
# update the palette to have the transparency color first
def updatePaletteTransparency(filename, transparency_str):
inFile = open(filename, "r")
inBuf = inFile.readlines()
inFile.close()
foundTransparency = False
counter = 0
outFile = open(filename, "w")
for l in inBuf:
# add the transparency as the first color
# the first three lines are headers
if counter == 3:
outFile.write(TRANSPARENCY_COLOR + "\r\n")
# skip over the first time
if l.startswith(TRANSPARENCY_COLOR) and foundTransparency == False:
foundTransparency = True
continue
outFile.write(l)
counter += 1
outFile.close()
if foundTransparency == False:
raise Exception("Failed to find transparency")
def main():
# delete existing temp TGA
try:
os.remove(TEMP_TGA_NAME)
except FileNotFoundError:
pass
# concatenate all of the .TGA files into a single one
os.system("convert -append *.TGA " + TEMP_TGA_NAME)
# extract the palette
os.system(IRFAN_PATH + " " + TEMP_TGA_NAME + " /bpp=8 /export_pal=\"" + PALETTE_NAME + "\" /killmesoftly")
# set the transparency color to index 0
updatePaletteTransparency(PALETTE_NAME, TRANSPARENCY_COLOR)
# set the palette on all TGAs
files = os.listdir(".")
for file in files:
if file.endswith(".TGA") != True:
continue
os.system(IRFAN_PATH + " " + file + "/import_pal=" + PALETTE_NAME + " /convert=\"" + file + "\" /killmesoftly")
return
if __name__ == '__main__':
main() |