|
1 | 1 | "use strict"; |
2 | 2 | Object.defineProperty(exports, "__esModule", { value: true }); |
3 | | -const Jimp = require("jimp"); |
4 | | -const fs = require("fs"); |
5 | | -const path = require("path"); |
6 | | -const outputFolder = "output"; |
7 | | -function sliceImage(filename, width, height) { |
8 | | - Jimp.read(filename, (err, image) => { |
9 | | - if (err) { |
10 | | - // Try again by appending '.png' to the filename |
11 | | - const pngFilename = `${filename}.png`; |
12 | | - Jimp.read(pngFilename, (pngErr, pngImage) => { |
13 | | - if (pngErr) { |
14 | | - console.error("Error reading the image:", pngErr); |
15 | | - return; |
16 | | - } |
17 | | - continueSlicing(pngImage, width, height, filename); |
18 | | - }); |
19 | | - } else { |
20 | | - continueSlicing(image, width, height, filename); |
21 | | - } |
22 | | - }); |
23 | | -} |
24 | | -function continueSlicing(image, width, height, inputFilename) { |
25 | | - // If height is not specified, use width as height |
26 | | - height = height || width; |
27 | | - const imageWidth = image.getWidth(); |
28 | | - const imageHeight = image.getHeight(); |
29 | | - // Calculate the number of slices in both dimensions |
30 | | - const horizontalSlices = Math.ceil(imageWidth / width); |
31 | | - const verticalSlices = Math.ceil(imageHeight / height); |
32 | | - // Create a folder for output if it doesn't exist |
33 | | - if (!fs.existsSync(outputFolder)) { |
34 | | - fs.mkdirSync(outputFolder); |
35 | | - } |
36 | | - // Slice the image and save each segment |
37 | | - for (let y = 0; y < verticalSlices; y++) { |
38 | | - for (let x = 0; x < horizontalSlices; x++) { |
39 | | - const startX = x * width; |
40 | | - const startY = y * height; |
41 | | - const sliceWidth = Math.min(width, imageWidth - startX); |
42 | | - const sliceHeight = Math.min(height, imageHeight - startY); |
43 | | - const slice = image.clone().crop(startX, startY, sliceWidth, sliceHeight); |
44 | | - // Incorporate the input filename into the output filename |
45 | | - const baseFilename = path.basename( |
46 | | - inputFilename, |
47 | | - path.extname(inputFilename), |
48 | | - ); |
49 | | - const outputFilename = `${outputFolder}/${baseFilename}_${x}_${y}.png`; |
50 | | - slice.write(outputFilename); |
51 | | - console.log(`Slice saved: ${outputFilename}`); |
52 | | - } |
| 3 | +const yargs = require("yargs"); |
| 4 | +const os = require("os"); |
| 5 | +const processImage_1 = require("./utils/processImage"); |
| 6 | +const processPath_1 = require("./utils/processPath"); |
| 7 | +// Parse command line arguments |
| 8 | +const options = yargs |
| 9 | + .option("f", { |
| 10 | + alias: "filename", |
| 11 | + describe: "Input image filename", |
| 12 | + type: "string", |
| 13 | + }) |
| 14 | + .option("i", { |
| 15 | + alias: "folderPath", |
| 16 | + describe: "Input folder", |
| 17 | + type: "string", |
| 18 | + }) |
| 19 | + .option("w", { |
| 20 | + alias: "width", |
| 21 | + describe: "Output image width", |
| 22 | + type: "number", |
| 23 | + demandOption: true, |
| 24 | + coerce: (value) => { |
| 25 | + if (value < 1) { |
| 26 | + throw new Error("width should not be lower than 1"); |
| 27 | + } |
| 28 | + return Math.round(value); |
| 29 | + }, |
| 30 | + }) |
| 31 | + .option("h", { |
| 32 | + alias: "height", |
| 33 | + describe: "Output image height", |
| 34 | + type: "number", |
| 35 | + coerce: (value) => { |
| 36 | + if (value !== undefined && value < 1) { |
| 37 | + throw new Error("height should not be lower than 1"); |
| 38 | + } |
| 39 | + return Math.round(value); |
| 40 | + }, |
| 41 | + }).argv; |
| 42 | +if (options.filename) { |
| 43 | + // Process a single image |
| 44 | + const { filename, width, height } = options; |
| 45 | + (0, processImage_1.sliceImage)(filename, width, height); |
| 46 | +} else if (options.folderPath) { |
| 47 | + // Process all images in a folder, splitting the task into threads |
| 48 | + let numCores = 2; |
| 49 | + try { |
| 50 | + numCores = os.cpus().length; |
| 51 | + } catch (err) { |
| 52 | + console.error(err); |
53 | 53 | } |
54 | | -} |
55 | | -// Get input from the command line arguments |
56 | | -const [filename, width, height] = process.argv.slice(2); |
57 | | -if (!filename || !width) { |
58 | | - console.log("Usage: node slice.cjs <filename> <width> [height]"); |
59 | | -} else { |
60 | | - sliceImage(filename, parseInt(width), parseInt(height)); |
| 54 | + numCores = Math.max(numCores - 1, 1); |
| 55 | + (0, processPath_1.processPath)(options.folderPath, options, numCores); |
61 | 56 | } |
0 commit comments