Skip to content

Instantly share code, notes, and snippets.

@acoyfellow
Created June 20, 2024 11:34
Show Gist options
  • Save acoyfellow/2362bdd2fa5daf49f56546297597fa63 to your computer and use it in GitHub Desktop.
Save acoyfellow/2362bdd2fa5daf49f56546297597fa63 to your computer and use it in GitHub Desktop.
Remove EXIF / Metadata from Images
const fs = require('fs').promises;
const path = require('path');
const sharp = require('sharp');
const sourceDir = './dirty';
const targetDir = './clean';
// Ensure target directory exists
async function ensureTargetDir() {
try {
await fs.mkdir(targetDir, { recursive: true });
} catch (err) {
console.error(`Error creating target directory: ${err.message}`);
}
}
// Process images
async function processImages() {
try {
// Ensure the target directory exists
await ensureTargetDir();
// Get all files in the source directory
const files = await fs.readdir(sourceDir);
for (const file of files) {
const sourceFile = path.join(sourceDir, file);
const targetFile = path.join(targetDir, file);
// Check if the file is an image
if (/\.(jpg|jpeg|png|webp|tiff|gif|bmp)$/i.test(file)) {
try {
// Remove EXIF/meta data and save to target directory
const data = await sharp(sourceFile).toBuffer();
await sharp(data).toFile(targetFile);
console.log(`Processed: ${file}`);
} catch (err) {
console.error(`Error processing file ${file}: ${err.message}`);
}
}
}
} catch (err) {
console.error(`Error processing images: ${err.message}`);
}
}
processImages();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment