-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateImages.mjs
More file actions
91 lines (80 loc) · 2.62 KB
/
Copy pathgenerateImages.mjs
File metadata and controls
91 lines (80 loc) · 2.62 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import fs from 'fs';
import path from 'path';
import { fetchURL, write } from 'image-js';
import { defaultImages, defaultMasks } from './imageDataset.mjs';
const __dirname = import.meta.dirname;
export async function imageLoader() {
const demoImagesDir = 'demoImages';
const staticDir = 'static';
const imageData = { masks: [], images: [] };
try {
// Create static directory if it doesn't exist
const staticPath = path.join(__dirname, staticDir, demoImagesDir);
if (!fs.existsSync(staticPath)) {
fs.mkdirSync(staticPath, { recursive: true });
}
const images = await Promise.all(
defaultImages.map((imageDataUrl) => fetchURL(imageDataUrl.value)),
);
for (let i = 0; i < images.length; i++) {
const image = images[i];
const imageDataUrl = defaultImages[i];
const imageTitle = getFilename(imageDataUrl.value);
const imagePath = path.join(
__dirname,
staticDir,
demoImagesDir,
'images',
imageTitle,
);
write(imagePath, image, { recursive: true });
// Keeping object structure for compatibility
imageData.images.push({
type: 'url',
imageType: 'image',
label: `${imageDataUrl.label} (${image.width}x${image.height})`,
value: `/${demoImagesDir}/images/${imageTitle}`,
});
}
const masks = await Promise.all(
defaultMasks.map((maskDataUrl) => fetchURL(maskDataUrl.value)),
);
for (let i = 0; i < masks.length; i++) {
const mask = masks[i];
const maskDataUrl = defaultMasks[i];
const maskTitle = getFilename(maskDataUrl.value);
const maskPath = path.join(
__dirname,
staticDir,
demoImagesDir,
'masks',
maskTitle,
);
write(maskPath, mask, { recursive: true });
// Keeping object structure for compatibility
imageData.masks.push({
type: 'url',
imageType: 'mask',
label: `${maskDataUrl.label} (${mask.width}x${mask.height})`,
value: `/${demoImagesDir}/masks/${maskTitle}`,
});
}
// Write data about newly created files.
const outputPath = path.join(__dirname, 'src/demo/contexts/demo/generated');
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath, { recursive: true });
}
fs.writeFileSync(
`${outputPath}/imageData.json`,
JSON.stringify(imageData, null, 2),
);
} catch (error) {
throw new Error(`Error in imageLoader: ${error.message}`);
}
return imageData;
}
// Returns only filename with extension.
function getFilename(filepath) {
return filepath.replace(/^.*[\\/]/, '');
}
await imageLoader();