Files
dusk/tools/assetstool/processimage.py
2025-08-25 10:16:55 -05:00

64 lines
1.8 KiB
Python

import os
from args import args
from PIL import Image
def extractPaletteFromImage(image):
# goes through and finds all unique colors in the image
if image.mode != 'RGBA':
image = image.convert('RGBA')
pixels = list(image.getdata())
uniqueColors = []
for color in pixels:
if color not in uniqueColors:
uniqueColors.append(color)
return uniqueColors
def savePalette(pixels, outputFilePath):
# Pixels is a list of (R, G, B, A) tuples
data = "DPF" # Header for Dusk Palette Format
# Count of colors (int32_t)
colorCount = len(pixels)
data += colorCount.to_bytes(4, byteorder='little').decode('latin1')
for r, g, b, a in pixels:
data += bytes([r, g, b, a]).decode('latin1')
os.makedirs(os.path.dirname(outputFilePath), exist_ok=True)
with open(outputFilePath, 'wb') as f:
f.write(data.encode('latin1'))
def processPalette(assetPath):
# Process the image file
print(f"Processing palette: {assetPath}")
# Load the image
image = Image.open(assetPath)
pixels = extractPaletteFromImage(image)
# Save the processed image to the output directory
fileNameWithoutExt = os.path.splitext(os.path.basename(assetPath))[0]
outputFilePath = os.path.join(args.output_assets, f"{fileNameWithoutExt}.dpf")
os.makedirs(os.path.dirname(outputFilePath), exist_ok=True)
savePalette(pixels, outputFilePath)
outputRelative = os.path.relpath(outputFilePath, args.output_assets)
return {
"outputFile": outputRelative,
"paletteColors": len(pixels),
"files": [ outputFilePath ]
}
def processImage(assetPath):
print(f"Processing image: {assetPath}")
# Load the image
image = Image.open(assetPath)
pixels = extractPaletteFromImage(image)
return {
# "outputFile": os.path.relpath(assetPath, args.input_assets),
# "paletteColors": len(pixels),
"files": [ assetPath ]
}