Files
dusk/tools/assetstool/processimage.py
2025-08-27 19:59:55 -05:00

60 lines
1.8 KiB
Python

import os
from args import args
from PIL import Image
import struct
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):
colorCount = len(pixels)
buf = bytearray(b"DPF")
buf += struct.pack("<i", colorCount) # little-endian int32_t
for r, g, b, a in pixels: # each channel 0..255
buf += struct.pack("BBBB", r, g, b, a) # raw bytes
os.makedirs(os.path.dirname(outputFilePath), exist_ok=True)
with open(outputFilePath, "wb") as f:
f.write(buf)
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 ]
}