Asset manager refactor begin.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import sys
|
||||
# from processtileset import processTileset
|
||||
# from processimage import processPalette, processImage
|
||||
from processimage import processImage
|
||||
from processpalette import processPalette
|
||||
|
||||
processedAssets = []
|
||||
@@ -15,8 +15,8 @@ def processAsset(asset):
|
||||
t = asset['type'].lower()
|
||||
if t == 'palette':
|
||||
return processPalette(asset)
|
||||
# elif t == 'image':
|
||||
# return processImage(asset)
|
||||
elif t == 'image':
|
||||
return processImage(asset)
|
||||
# elif t == 'tileset':
|
||||
# return processTileset(asset)
|
||||
else:
|
||||
|
78
tools/assetstool/processimage.py
Normal file
78
tools/assetstool/processimage.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import os
|
||||
import sys
|
||||
from PIL import Image
|
||||
from processpalette import extractPaletteFromImage, palettes
|
||||
from args import args
|
||||
from assethelpers import getAssetRelativePath
|
||||
|
||||
images = []
|
||||
|
||||
def processImage(asset):
|
||||
type = asset['options'].get('type', 'PALETTIZED').upper()
|
||||
if type == 'PALETTIZED':
|
||||
return processPalettizedImage(asset)
|
||||
elif type == 'ALPHA':
|
||||
return processAlphaImage(asset)
|
||||
else:
|
||||
print(f"Error: Unknown image type {type} for asset {asset['path']}")
|
||||
sys.exit(1)
|
||||
|
||||
def processPalettizedImage(asset):
|
||||
assetPath = asset['path']
|
||||
|
||||
image = Image.open(assetPath)
|
||||
imagePalette = extractPaletteFromImage(image)
|
||||
|
||||
# Find palette that contains every color
|
||||
for palette in palettes:
|
||||
if all(color in palette['pixels'] for color in imagePalette):
|
||||
break
|
||||
else:
|
||||
print(f"No matching palette found for {assetPath}!")
|
||||
# Find which pixel is missing
|
||||
for color in imagePalette:
|
||||
if color not in palette:
|
||||
print(f"Missing color: {color}")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"Converting image {assetPath} to use palette")
|
||||
|
||||
paletteIndexes = []
|
||||
for pixel in list(image.getdata()):
|
||||
if pixel[3] == 0:
|
||||
pixel = (0, 0, 0, 0)
|
||||
paletteIndex = palette['pixels'].index(pixel)
|
||||
paletteIndexes.append(paletteIndex)
|
||||
|
||||
data = bytearray()
|
||||
data.extend(b"DPI") # Dusk Palettized Image
|
||||
data.extend(image.width.to_bytes(4, 'little')) # Width
|
||||
data.extend(image.height.to_bytes(4, 'little')) # Height
|
||||
data.append(palette['paletteIndex']) # Palette index
|
||||
for paletteIndex in paletteIndexes:
|
||||
if paletteIndex > 255 or paletteIndex < 0:
|
||||
print(f"Error: Palette index {paletteIndex} exceeds 255!")
|
||||
sys.exit(1)
|
||||
data.append(paletteIndex.to_bytes(1, 'little')[0]) # Pixel index
|
||||
|
||||
relative = getAssetRelativePath(assetPath)
|
||||
fileNameWithoutExt = os.path.splitext(os.path.basename(assetPath))[0]
|
||||
outputFileRelative = os.path.join(os.path.dirname(relative), f"{fileNameWithoutExt}.dpi")
|
||||
outputFilePath = os.path.join(args.output_assets, outputFileRelative)
|
||||
os.makedirs(os.path.dirname(outputFilePath), exist_ok=True)
|
||||
with open(outputFilePath, "wb") as f:
|
||||
f.write(data)
|
||||
|
||||
outImage = {
|
||||
"files": [ outputFilePath ],
|
||||
}
|
||||
return outImage
|
||||
|
||||
def processAlphaImage(asset):
|
||||
assetPath = asset['path']
|
||||
print(f"Processing alpha image: {assetPath}")
|
||||
|
||||
outImage = {
|
||||
"files": []
|
||||
}
|
||||
return outImage
|
@@ -13,7 +13,7 @@ def extractPaletteFromImage(image):
|
||||
pixels = list(image.getdata())
|
||||
uniqueColors = []
|
||||
for color in pixels:
|
||||
# We treat alpha 0 as rgba(0,0,0,0) for palette purposes
|
||||
# We treat all alpha 0 as rgba(0,0,0,0) for palette purposes
|
||||
if color[3] == 0:
|
||||
color = (0, 0, 0, 0)
|
||||
if color not in uniqueColors:
|
||||
|
Reference in New Issue
Block a user