-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-image-list.js
63 lines (50 loc) · 1.78 KB
/
generate-image-list.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
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// Directory containing the images
const imagesDir = path.join(__dirname, 'downloaded_images');
// Output file path
const outputFile = path.join(__dirname, 'image-list.js');
// Function to generate the image list
function generateImageList() {
try {
// Check if the directory exists
if (!fs.existsSync(imagesDir)) {
console.error(`Directory not found: ${imagesDir}`);
process.exit(1);
}
// Read all files in the directory
const files = fs.readdirSync(imagesDir);
// Filter for JPEG files
const jpegFiles = files.filter(file =>
file.toLowerCase().endsWith('.jpg') ||
file.toLowerCase().endsWith('.jpeg')
);
// Create the image list
const imageList = jpegFiles.map(jpegFile => {
// Get the base name without extension
const baseName = path.parse(jpegFile).name;
// Check if corresponding .txt file exists
const txtFile = `${baseName}.txt`;
const txtPath = path.join(imagesDir, txtFile);
// Only include entries where both JPEG and TXT files exist
if (fs.existsSync(txtPath)) {
return {
image: `downloaded_images/${jpegFile}`,
description: `downloaded_images/${txtFile}`
};
}
return null;
}).filter(item => item !== null);
// Create the JavaScript content
const jsContent = `const imageList = ${JSON.stringify(imageList, null, 2)};\n`;
// Write the JavaScript file
fs.writeFileSync(outputFile, jsContent);
console.log(`Successfully generated ${outputFile} with ${imageList.length} images`);
} catch (error) {
console.error('Error generating image list:', error);
process.exit(1);
}
}
// Run the function
generateImageList();