DMI — Icon Parsing

class avulto.DMI

A DMI file.

The DMI class provides the ability to create, parse, and modify *.dmi files.

Instances are created with the following methods:

static from_file(filename: os.PathLike[str] | str) → DMI

Creates a DMI from the given filename.

static new(dims: tuple[int, int]) → DMI

Creates an empty DMI file with the given icon width-height tuple dims.

Once instantiated, the following methods and properties are available:

property filepath: pathlib.Path | None

The original filename of the DMI.

property icon_width: int

The width of icons in the DMI.

property icon_height: int

The height of icons in the DMI.

property icon_dims: tuple[int, int]

The width and height of icons in the DMI.

property states: list[IconState]

The states in the DMI, in declaration order.

state_names() → list[str]

Return a list of strings containing all state names in the file.

state(value: str) → IconState

Return the icon state with the given name. If there are duplicates, only the first one is returned. Use states to retrieve duplicates.

save_to(filename: Any, compress: int = 0) → None

Saves the DMI to the given filename.

compress is an value from 0 to 6, corresponding to oxipng optimization values.

class avulto.IconState

A single icon state in a DMI file.

static from_data(data: Mapping[Dir, Sequence[bytes]], width: int = 32, height: int = 32, name: str = '', delays: Sequence[float] | None = None, loops: int = 0, rewind: bool = False, movement: bool = False) → IconState

Construct an IconState with the given data and other arguments.

data must be a dict mapping Dir values to lists of bytes containing RGBA image data. The number of images in each list must be the same, and if there is more than one frame in the icon state, delays must contain the same number of delay intervals as there are images in the state.

property name: str

The state name. May not be unique within a file.

property frames: int

The number of frames in the icon state.

property delay: list[float]

The icon’s frame delays, in ticks.

property dirs: list[Dir]

The directions available in the icon state.

property movement: bool

Whether or not the state is a movement state.

property rewind: bool

Whether the icon animates both backwards and forwards.

data_rgba8(frame: int, dir: Any) → bytes

Return the byte data of the spritesheet in 8-bit RGBA.

Using Pillow, the image data for a given icon can quickly be turned into a PIL.Image object and easily manipulated.

from avulto import DMI, Dir
from PIL import Image
dmi = DMI.from_file("/SS13/icons/objects/weapons.dmi")
pistol = dmi.state("pistol")
data = pistol.data_rgba8(frame=1, dir=Dir.SOUTH)
image = Image.frombytes("RGBA", size=dmi.icon_dims, data=data)