Command Line Palette Processing?

slinga

Established Member
I'm trying to improve my sprite workflow. I need to the following:

1) Combine all of my sprites
2) Create a single optimized palette of 255 colors
3) Export that palette
4) Apply that palette to each of my sprites

So far I have this (using ImageMagick from the command line)

Code:
rm ALL.TGA
convert -append *.TGA  ALL.TGA
convert ALL.TGA -depth 8 -colors 255 ALL2.TGA

The append appends all of my sprites into a single file 24-bit color TGA. The next line creates an indexed (palette) TGA but instead of using 255 colors it uses ~16 or so.

I then need a way to extract the palette from ALL2.TGA and apply it to each of my sprites. Any advice on how to do this? Thanks in advance.
 
you can do this with Irfanview. it has command line options, and I think that includes palettes - it's free to use as well.

from i_options.txt:
/import_pal=palfile - import and apply a special palette to the image (PAL format)
/export_pal=palfile - export image palette to file (PAL format)

this is a script I made to open all .png images in a folder, convert them to 8bpp, and then export the .pal file - importing the palette should be similar.

@echo off
"C:\Program Files (x86)\IrfanView\i_view32.exe" /bpp=8 /export_pal="B:\SteamLibrary\steamapps\common\Sonic Mania\Data\Data\Images\TrueEnd.pal" /convert="B:\SteamLibrary\steamapps\common\Sonic Mania\Data\Data\Images\*.tga" /file="B:\SteamLibrary\steamapps\common\Sonic Mania\Data\Data\Images\TrueEnd.png"
 
Last edited:
Thanks for the fast response. I've been using IrfanView via the UI. I'll look into the command line option.
 
This is close to what I want:

Code:
# Take a directory of .TGAs, create a single 8BPP palette, and then apply that palette to all of the .TGA files

# concatenate all of the .TGA files into a single one
rm _ALL_TEMP.TGA
convert -append *.TGA  _ALL_TEMP.TGA

# extract the palette
wine ~/.wine/drive_c/Program\ Files\ \(x86\)/IrfanView/i_view32.exe _ALL_TEMP.TGA /bpp=8 /export_pal="PALETTE.PAL" /killmesoftly

# Apply the palette to each TGA in the dir
for file in *; do

    if [[ ${file##*.} == "TGA" ]]; then
        if [[ -f "$file" ]]; then
            wine ~/.wine/drive_c/Program\ Files\ \(x86\)/IrfanView/i_view32.exe $file /import_pal=PALETTE.PAL /convert="$file" /killmesoftly
        fi
    fi
done

Mix of bash + wine which is gross. The only issue I have is that the transparency color seems to move around on me. This is annoying because I have to manually set the transparency index when I make calls to jo_sprite_add_tga_tileset(). Otherwise this seems to work for me needs. Thanks again.
 
hmm, mine is always palette index 0 or 1, but I basically manage this manually. PALETTE.PAL is the same for every file right? or is it the transparent index for the generated palette that changes every time you run the script?

could it be the order of the concatenated TGAs isn't the same every time?
 
I converted my script to Python to handle adjusting the transparency color.

Python:
"""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()
 
Back
Top