sprites/tools/trim/image.ts
Christopher Monsanto ad51a0464b ts trim
2020-08-07 04:34:10 -04:00

38 lines
1.2 KiB
TypeScript

import cp from 'child_process';
export function getDims(input : string) {
const info = cp.execFileSync('convert', [input, '-format', "%w+%h+%@", 'info:'],
{encoding:'utf8'});
const [imageWidth, imageHeight, width, height, left, top] =
info.split(/x|\+/g).map(dim => parseInt(dim));
const right = imageWidth - (left + width);
const bottom = imageHeight - (top + height);
return {
width,
height,
left,
top,
right,
bottom
}
}
export function crop(input : string, {width, height, left, top} : {width : number, height: number, left: number, top: number}, output : string) {
cp.execFileSync('convert', [input, '+repage', '-crop', `${width}x${height}+${left}+${top}`, output]);
}
// Trim, preserving displacement from center
// Returns crop coords
export function losslessTrim(dims : {width : number, height: number, left: number, top: number, bottom: number, right: number}) {
return {
left: Math.min(dims.left, dims.right),
width: dims.width + Math.abs(dims.left - dims.right),
top: Math.min(dims.top, dims.bottom),
height: dims.height + Math.abs(dims.top - dims.bottom),
}
}