MCPcopy Create free account
hub / github.com/AntonPalmqvist/physically-based-api / processImages

Function processImages

scripts/image-processor.mjs:13–106  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11const renderFolder = process.argv[2] || "cycles";
12
13async function processImages() {
14 try {
15 // Define the folder containing the original images
16 const folderPath = `images/renders/${renderFolder}/original`;
17
18 // Define the folders to save resized images
19 const resizedFolderPath300 = `images/renders/${renderFolder}/300`;
20 const resizedFolderPath600 = `images/renders/${renderFolder}/600`;
21
22 // Check if the resized folders exist, if not, create them
23 if (!fs.existsSync(resizedFolderPath300)) {
24 fs.mkdirSync(resizedFolderPath300, { recursive: true });
25 }
26 if (!fs.existsSync(resizedFolderPath600)) {
27 fs.mkdirSync(resizedFolderPath600, { recursive: true });
28 }
29
30 // Read the contents of the folder
31 const files = await fs.promises.readdir(folderPath);
32
33 // Loop through each file
34 for (const file of files) {
35 // Check if the file is an image (you may need to refine this check)
36 if (file.toLowerCase().endsWith(".png")) {
37 // Construct the full path to the image file
38 const imagePath = path.join(folderPath, file);
39
40 // JPEG
41 // Process for JPEG 600x600
42 const outputFileNameJpeg600 = `${path.parse(file).name}.jpeg`;
43 const outputFileJpeg600 = path.join(
44 resizedFolderPath600,
45 outputFileNameJpeg600,
46 );
47
48 const processJpeg600 = await sharp(imagePath)
49 .withMetadata() // Keeps most metadata and adds sRGB ICC profile https://sharp.pixelplumbing.com/api-output#withmetadata
50 .resize({ width: 600, height: 600 })
51 .toFormat("jpeg", { mozjpeg: true, quality: 65 })
52 .toFile(outputFileJpeg600); // Save resized image with modified name
53
54 console.log(`Processed ${file} (600x600):`, processJpeg600);
55
56 // Process for JPEG 300x300
57 const outputFileNameJpeg300 = `${path.parse(file).name}.jpeg`;
58 const outputFileJpeg300 = path.join(
59 resizedFolderPath300,
60 outputFileNameJpeg300,
61 );
62
63 const processJpeg300 = await sharp(imagePath)
64 .withMetadata() // Keeps most metadata and adds sRGB ICC profile https://sharp.pixelplumbing.com/api-output#withmetadata
65 .resize({ width: 300, height: 300 })
66 .toFormat("jpeg", { mozjpeg: true, quality: 65 })
67 .toFile(outputFileJpeg300); // Save resized image with modified name
68
69 console.log(`Processed ${file} (300x300):`, processJpeg300);
70

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected