-
Notifications
You must be signed in to change notification settings - Fork 0
/
blur-images.js
51 lines (43 loc) · 1.34 KB
/
blur-images.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
// Define the input and output directories
const inputDir = './input';
const outputDir = './output';
// Create the output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
// Function to apply blur to an image
function applyBlur(inputFilePath, outputFilePath, blurAmount) {
sharp(inputFilePath)
.blur(blurAmount)
.toFile(outputFilePath, (err, info) => {
if (err) {
console.error(`Error applying blur to ${inputFilePath}: ${err}`);
} else {
console.log(
`Applied blur to ${inputFilePath} and saved at ${outputFilePath}`
);
}
});
}
// Read the files in the input directory
fs.readdir(inputDir, (err, files) => {
if (err) {
console.error(`Error reading input directory: ${err}`);
return;
}
// Filter only image files (JPG, PNG, WebP, SVG)
const imageFiles = files.filter((file) =>
/\.(jpg|jpeg|png|webp)$/i.test(file)
);
// Apply blur to each image
imageFiles.forEach((file) => {
const inputFilePath = path.join(inputDir, file);
const outputFilePath = path.join(outputDir, file);
// Adjust the blur amount as needed (e.g., 5 for mild blur)
const blurAmount = 25;
applyBlur(inputFilePath, outputFilePath, blurAmount);
});
});