Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const path = require('path');
- const Jimp = require('jimp');
- const inputDir = 'inPut/';
- const outputDir = 'outPut/';
- // Function to process each image in the directory
- async function processImage(imagePath) {
- try {
- // Load the image with Jimp
- const image = await Jimp.read(imagePath);
- // Store the original width and height
- const originalWidth = image.bitmap.width;
- const originalHeight = image.bitmap.height;
- // Set the width and height back to the original values
- image.resize(originalWidth, originalHeight);
- // Save the modified image to the output directory
- const outputFilePath = path.join(outputDir, path.basename(imagePath));
- await image.writeAsync(outputFilePath);
- console.log(`Image ${path.basename(imagePath)} processed successfully.`);
- } catch (error) {
- console.error(`Error processing image ${path.basename(imagePath)}:`, error);
- }
- }
- // Function to process all images in the input directory
- async function processImages() {
- try {
- const imageFiles = fs.readdirSync(inputDir);
- // Filter only image files (you can extend this check if needed)
- const imageFilesFiltered = imageFiles.filter((file) =>
- ['.jpg', '.jpeg', '.png', '.gif', '.bmp'].includes(path.extname(file).toLowerCase())
- );
- for (const imageFile of imageFilesFiltered) {
- const imagePath = path.join(inputDir, imageFile);
- await processImage(imagePath);
- }
- } catch (error) {
- console.error('Error processing images:', error);
- }
- }
- // Call the main function to process all images in the input directory
- processImages();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement