Skip to content

Commit c59bad4

Browse files
committed
Initial push
1 parent 39cc533 commit c59bad4

File tree

6 files changed

+762
-0
lines changed

6 files changed

+762
-0
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Exclude files from archive
5+
.gitattributes export-ignore

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Diagnostic reports (https://nodejs.org/api/report.html)
7+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
8+
9+
# Dependency directories
10+
node_modules/
11+
12+
# TypeScript cache
13+
*.tsbuildinfo
14+
15+
# OS
16+
.DS_Store
17+
Thumbs.db
18+
19+
# Images
20+
*.jpg
21+
*.jpeg
22+
*.png
23+
*.bmp
24+
*.tiff
25+
*.gif

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# node-image-slice
2+
3+
Command-line utility that slices an input image into segments according to specified width and height, and outputs those segments into a folder.
4+
5+
## Usage
6+
7+
`node slice.cjs <filename> <width> [height]`
8+
9+
- If `filename` does not include an extension, `.png` will be guessed
10+
- If `height` is not specified, `width` will be used as `height`
11+
12+
## Supported formats
13+
14+
- jpeg
15+
- png
16+
- bmp
17+
- tiff
18+
- gif
19+
20+
## Background
21+
22+
Created with Chat-GPT 3.5.

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "node-image-slice",
3+
"version": "1.0.0",
4+
"description": "Slices an input image into segments according to specified width and height",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/thinknathan/node-image-slice.git"
8+
},
9+
"author": "Nathan Bolton (https://thinknathan.ca/)",
10+
"license": "CC0-1.0",
11+
"type": "commonjs",
12+
"main": "slice.cjs",
13+
"files": [
14+
"slice.cjs"
15+
],
16+
"scripts": {
17+
"prettier": "prettier \"./**/*.{cjs,md,json}\" --write"
18+
},
19+
"devDependencies": {
20+
"jimp": "~0.22.10",
21+
"prettier": "^3.1.1"
22+
}
23+
}

slice.cjs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env node
2+
3+
const Jimp = require("jimp");
4+
const fs = require("fs");
5+
const path = require("path"); // Import the 'path' module for working with file paths
6+
7+
const outputFolder = "output";
8+
9+
function sliceImage(filename, width, height) {
10+
Jimp.read(filename, (err, image) => {
11+
if (err) {
12+
// Try again by appending '.png' to the filename
13+
const pngFilename = `${filename}.png`;
14+
Jimp.read(pngFilename, (pngErr, pngImage) => {
15+
if (pngErr) {
16+
console.error("Error reading the image:", pngErr);
17+
return;
18+
}
19+
continueSlicing(pngImage, width, height, filename);
20+
});
21+
} else {
22+
continueSlicing(image, width, height, filename);
23+
}
24+
});
25+
}
26+
27+
function continueSlicing(image, width, height, inputFilename) {
28+
// If height is not specified, use width as height
29+
height = height || width;
30+
31+
const imageWidth = image.getWidth();
32+
const imageHeight = image.getHeight();
33+
34+
// Calculate the number of slices in both dimensions
35+
const horizontalSlices = Math.ceil(imageWidth / width);
36+
const verticalSlices = Math.ceil(imageHeight / height);
37+
38+
// Create a folder for output if it doesn't exist
39+
if (!fs.existsSync(outputFolder)) {
40+
fs.mkdirSync(outputFolder);
41+
}
42+
43+
// Slice the image and save each segment
44+
for (let y = 0; y < verticalSlices; y++) {
45+
for (let x = 0; x < horizontalSlices; x++) {
46+
const startX = x * width;
47+
const startY = y * height;
48+
49+
const sliceWidth = Math.min(width, imageWidth - startX);
50+
const sliceHeight = Math.min(height, imageHeight - startY);
51+
52+
const slice = image.clone().crop(startX, startY, sliceWidth, sliceHeight);
53+
54+
// Incorporate the input filename into the output filename
55+
const baseFilename = path.basename(
56+
inputFilename,
57+
path.extname(inputFilename),
58+
);
59+
const outputFilename = `${outputFolder}/${baseFilename}_${x}_${y}.png`;
60+
61+
slice.write(outputFilename);
62+
console.log(`Slice saved: ${outputFilename}`);
63+
}
64+
}
65+
}
66+
67+
// Get input from the command line arguments
68+
const [filename, width, height] = process.argv.slice(2);
69+
70+
if (!filename || !width) {
71+
console.log("Usage: node sliceImage.js <filename> <width> [height]");
72+
} else {
73+
sliceImage(filename, parseInt(width), parseInt(height));
74+
}

0 commit comments

Comments
 (0)