BosonX/converters.py

60 lines
1.5 KiB
Python

import fxconv
from PIL import Image, ImageChops
def convert(input, output, params, target):
recognized = True
if params["custom-type"] == "animation":
o = convert_animation(input, params)
else:
recognized = False
if recognized:
fxconv.elf(o, output, "_" + params["name"], **target)
return 0
return 1
def convert_animation(input, params):
img = Image.open(input)
frame_count = 0
image_array = fxconv.ObjectData()
x_array = bytes()
y_array = bytes()
bg = Image.new("RGBA", img.size, (0, 0, 0, 0))
if not "anchor" in params:
raise fxconv.FxconvError(f"no anchor specified for {input}")
x_anchor, y_anchor = map(int, params["anchor"].split(","))
while 1:
try:
img.seek(frame_count)
except EOFError:
break
# Trim image
nonzero = ImageChops.difference(img.convert("RGBA"), bg)
bbox = nonzero.getbbox()
if bbox:
frame = img.crop(bbox)
x, y = bbox[0], bbox[1]
else:
frame = img.copy()
x, y = 0, 0
image_array += fxconv.ptr(fxconv.convert_bopti_cg(frame, params))
x_array += fxconv.u8(x)
y_array += fxconv.u8(y)
frame_count += 1
o = fxconv.ObjectData()
o += fxconv.u32(frame_count)
o += fxconv.ptr(image_array)
o += fxconv.ptr(x_array)
o += fxconv.ptr(y_array)
o += fxconv.u16(x_anchor)
o += fxconv.u16(y_anchor)
return o