Skip to content

Commit 784316b

Browse files
committed
Implement final canvas sizing
1 parent 699a10e commit 784316b

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ Command-line utility that slices input images into segments according to specifi
1919
`node slice.cjs`
2020

2121
```
22-
-f, --filename Input image filename [string]
23-
-i, --folderPath Input folder [string]
24-
-w, --width Output image width [number] [required]
25-
-h, --height Output image height [number]
22+
-f, --filename Input image filename [string]
23+
-i, --folderPath Input folder [string]
24+
-w, --width Width of each slice [number] [required]
25+
-h, --height Height of each slice [number]
26+
-d, --canvasWidth Width of canvas for final output [number]
27+
-g, --canvasHeight Height of canvas for final output [number]
2628
```
2729

2830
- If `filename` does not include an extension, `.png`, `.gif`, `.jpg` and `.jpeg` will be guessed

slice.cjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ const options = yargs
4242
})
4343
.option('d', {
4444
alias: 'canvasWidth',
45-
describe: 'Width of the canvas',
45+
describe: 'Width of canvas for final output',
4646
type: 'number',
4747
})
4848
.option('g', {
4949
alias: 'canvasHeight',
50-
describe: 'Height of the canvas',
50+
describe: 'Height of canvas for final output',
5151
type: 'number',
5252
}).argv;
5353
if (options.filename) {

src/utils/processImage.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,27 @@ function continueSlicing(
108108
);
109109
const outputFilename = `${outputFolder}/${baseFilename}_${x}_${y}.png`;
110110

111-
slice.write(outputFilename);
111+
if (canvasWidth || canvasHeight) {
112+
// Calculate canvas dimensions
113+
const finalCanvasWidth = canvasWidth || width;
114+
const finalCanvasHeight = (canvasHeight || canvasWidth) ?? height;
115+
116+
// Create a new canvas with transparent background
117+
const canvas = new Jimp(
118+
finalCanvasWidth,
119+
finalCanvasHeight,
120+
0x00000000,
121+
);
122+
123+
// Composite the image in the middle of the canvas
124+
const startX2 = Math.floor((finalCanvasWidth - sliceWidth) / 2);
125+
const startY2 = Math.floor((finalCanvasHeight - sliceHeight) / 2);
126+
canvas.composite(slice, startX2, startY2);
127+
canvas.write(outputFilename);
128+
} else {
129+
slice.write(outputFilename);
130+
}
131+
112132
console.log(`Slice saved: ${outputFilename}`);
113133
}
114134
}

utils/processImage.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,21 @@ function continueSlicing(image, width, height, canvasWidth, canvasHeight, inputF
6868
// Incorporate the input filename into the output filename
6969
const baseFilename = path.basename(inputFilename, path.extname(inputFilename));
7070
const outputFilename = `${outputFolder}/${baseFilename}_${x}_${y}.png`;
71-
slice.write(outputFilename);
71+
if (canvasWidth || canvasHeight) {
72+
// Calculate canvas dimensions
73+
const finalCanvasWidth = canvasWidth || width;
74+
const finalCanvasHeight = (canvasHeight || canvasWidth) ?? height;
75+
// Create a new canvas with transparent background
76+
const canvas = new Jimp(finalCanvasWidth, finalCanvasHeight, 0x00000000);
77+
// Composite the image in the middle of the canvas
78+
const startX2 = Math.floor((finalCanvasWidth - sliceWidth) / 2);
79+
const startY2 = Math.floor((finalCanvasHeight - sliceHeight) / 2);
80+
canvas.composite(slice, startX2, startY2);
81+
canvas.write(outputFilename);
82+
}
83+
else {
84+
slice.write(outputFilename);
85+
}
7286
console.log(`Slice saved: ${outputFilename}`);
7387
}
7488
}

0 commit comments

Comments
 (0)