Advertisement
yoav_tc

Mass convert PPI

Aug 4th, 2023
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.63 KB | Source Code | 0 0
  1. const fs = require('fs');
  2. const path = require('path');
  3. const Jimp = require('jimp');
  4.  
  5. const inputDir = 'inPut/';
  6. const outputDir = 'outPut/';
  7.  
  8. // Function to process each image in the directory
  9. async function processImage(imagePath) {
  10.   try {
  11.     // Load the image with Jimp
  12.     const image = await Jimp.read(imagePath);
  13.  
  14.     // Store the original width and height
  15.     const originalWidth = image.bitmap.width;
  16.     const originalHeight = image.bitmap.height;
  17.  
  18.     // Set the width and height back to the original values
  19.     image.resize(originalWidth, originalHeight);
  20.  
  21.     // Save the modified image to the output directory
  22.     const outputFilePath = path.join(outputDir, path.basename(imagePath));
  23.     await image.writeAsync(outputFilePath);
  24.  
  25.     console.log(`Image ${path.basename(imagePath)} processed successfully.`);
  26.   } catch (error) {
  27.     console.error(`Error processing image ${path.basename(imagePath)}:`, error);
  28.   }
  29. }
  30.  
  31. // Function to process all images in the input directory
  32. async function processImages() {
  33.   try {
  34.     const imageFiles = fs.readdirSync(inputDir);
  35.  
  36.     // Filter only image files (you can extend this check if needed)
  37.     const imageFilesFiltered = imageFiles.filter((file) =>
  38.       ['.jpg', '.jpeg', '.png', '.gif', '.bmp'].includes(path.extname(file).toLowerCase())
  39.     );
  40.  
  41.     for (const imageFile of imageFilesFiltered) {
  42.       const imagePath = path.join(inputDir, imageFile);
  43.       await processImage(imagePath);
  44.     }
  45.   } catch (error) {
  46.     console.error('Error processing images:', error);
  47.   }
  48. }
  49.  
  50. // Call the main function to process all images in the input directory
  51. processImages();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement