Skip to content

Commit b14c65e

Browse files
committed
fix: minor updates and fixes
1 parent 2347209 commit b14c65e

File tree

4 files changed

+70
-192
lines changed

4 files changed

+70
-192
lines changed

docusaurus.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ async function createConfig() {
5353
baseUrl: '/',
5454

5555
plugins: [
56-
function generateImagesPlugin(context) {
56+
function generateImagesPlugin() {
5757
return {
5858
name: 'generate-images-plugin',
5959
async loadContent() {
6060
const { imageLoader } = await import('./imageLoader.js');
61-
await imageLoader(context.siteDir);
61+
await imageLoader();
6262
},
6363
};
6464
},

imageDataset.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,53 @@ const morphologyMaskUrl = 'https://demo-dataset.image-js.org/morphology_mask';
44

55
export const defaultImages = [
66
{
7-
type: 'url',
87
value: `${standardUrl}/jellybeans.png`,
98
label: 'Jelly beans ',
109
imageType: 'image',
1110
},
1211
{
13-
type: 'url',
1412
value: `${standardUrl}/female.png`,
1513
label: 'Female',
1614
imageType: 'image',
1715
},
1816
{
19-
type: 'url',
2017
value: `${standardUrl}/femaleBellLabs.png`,
2118
label: 'Female from Bell Labs ',
2219
imageType: 'image',
2320
},
2421
{
25-
type: 'url',
2622
value: `${standardUrl}/house.png`,
2723
label: 'House',
2824
imageType: 'image',
2925
},
3026
{
31-
type: 'url',
3227
value: `${standardUrl}/mandrill.png`,
3328
label: 'Mandrill',
3429
imageType: 'image',
3530
},
3631
{
37-
type: 'url',
3832
value: `${standardUrl}/peppers.png`,
3933
label: 'Peppers',
4034
imageType: 'image',
4135
},
4236

4337
{
44-
type: 'url',
4538
value: `${standardUrl}/barbara.jpg`,
4639
label: 'Barbara',
4740
imageType: 'image',
4841
},
4942

5043
{
51-
type: 'url',
5244
value: `${standardUrl}/boat.png`,
5345
label: 'Standard boat',
5446
imageType: 'image',
5547
},
5648
{
57-
type: 'url',
5849
value: `${standardUrl}/male.png`,
5950
label: 'Male',
6051
imageType: 'image',
6152
},
6253
{
63-
type: 'url',
6454
value: `${standardUrl}/airport.png`,
6555
label: 'Airport',
6656
imageType: 'image',
@@ -75,51 +65,43 @@ export const defaultMasks = [
7565
imageType: 'mask',
7666
},
7767
{
78-
type: 'url',
7968
value: `${morphologyMaskUrl}/shapes.png`,
8069
label: 'Shapes',
8170
imageType: 'mask',
8271
},
8372
{
84-
type: 'url',
8573
value: `${morphologyMaskUrl}/star.png`,
8674
label: 'Star',
8775
imageType: 'mask',
8876
},
8977
{
90-
type: 'url',
9178
value: `${standardMaskUrl}/house.png`,
9279
label: 'House',
9380
imageType: 'mask',
9481
},
9582
{
96-
type: 'url',
9783
value: `${standardMaskUrl}/mandrill.png`,
9884
label: 'Mandrill',
9985
imageType: 'mask',
10086
},
10187
{
102-
type: 'url',
10388
value: `${standardMaskUrl}/peppers.png`,
10489
label: 'Peppers',
10590
imageType: 'mask',
10691
},
10792

10893
{
109-
type: 'url',
11094
value: `${standardMaskUrl}/barbara.png`,
11195
label: 'Barbara',
11296
imageType: 'mask',
11397
},
11498
{
115-
type: 'url',
11699
value: `${standardMaskUrl}/boat.png`,
117-
label: 'Standard boat)',
100+
label: 'Standard boat',
118101
imageType: 'mask',
119102
},
120103

121104
{
122-
type: 'url',
123105
value: `${standardMaskUrl}/male.png`,
124106
label: 'Male',
125107
imageType: 'mask',

imageLoader.js

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,70 @@
1-
const fs = require('fs');
2-
const imageJs = require('image-js');
3-
const { fetchURL } = require('image-js');
4-
5-
const { defaultImages, defaultMasks } = require('./imageDataset.js');
6-
7-
async function imageLoader(siteDir) {
8-
const imageData = [];
9-
for (let imageDataUrl of defaultImages) {
10-
const image = await fetchURL(imageDataUrl.value);
11-
const imageTitle = imageDataUrl.value.slice(
12-
imageDataUrl.value.lastIndexOf('/'),
1+
import fs from 'fs';
2+
3+
import { fetchURL, write } from 'image-js';
4+
5+
import { defaultImages, defaultMasks } from './imageDataset.js';
6+
7+
export async function imageLoader() {
8+
const staticDir = '/demoImages/';
9+
10+
const imageData = { masks: [], images: [] };
11+
try {
12+
// Create static directory if it doesn't exist
13+
if (!fs.existsSync(`./static${staticDir}`)) {
14+
fs.mkdirSync(`./static${staticDir}`, { recursive: true });
15+
}
16+
17+
const images = await Promise.all(
18+
defaultImages.map((imageDataUrl) => fetchURL(imageDataUrl.value)),
1319
);
14-
imageJs.write(`./static/${imageTitle}`, image);
15-
16-
imageData.push({
17-
type: 'image',
18-
label: imageDataUrl.label,
19-
width: image.width,
20-
height: image.height,
21-
path: path.concat(
22-
imageDataUrl.value.slice(imageDataUrl.value.lastIndexOf('/')),
23-
),
24-
});
25-
}
2620

27-
for (const maskDataUrl of defaultMasks) {
28-
const mask = await fetchURL(maskDataUrl.value);
29-
const maskTitle = maskDataUrl.value.slice(
30-
maskDataUrl.value.lastIndexOf('/'),
21+
for (let i = 0; i < images.length; i++) {
22+
const image = images[i];
23+
const imageDataUrl = defaultImages[i];
24+
const imageTitle = imageDataUrl.value.slice(
25+
imageDataUrl.value.lastIndexOf('/') + 1,
26+
);
27+
write(`./static${staticDir}images/${imageTitle}`, image, {
28+
recursive: true,
29+
});
30+
31+
imageData.images.push({
32+
type: 'url',
33+
imageType: 'image',
34+
label: `${imageDataUrl.label} (${image.width}x${image.height})`,
35+
value: `${staticDir}images/${imageTitle}`,
36+
});
37+
}
38+
39+
// Fetch all masks in parallel
40+
const masks = await Promise.all(
41+
defaultMasks.map((maskDataUrl) => fetchURL(maskDataUrl.value)),
3142
);
32-
imageJs.write(`./static/${maskTitle}`, mask);
33-
imageData.push({
34-
type: 'mask',
35-
label: maskDataUrl.label,
36-
width: mask.width,
37-
height: mask.height,
38-
path: path.concat(
39-
maskDataUrl.value.slice(maskDataUrl.value.lastIndexOf('/')),
40-
),
41-
});
42-
}
4343

44-
// Use absolute path
45-
const outputPath = siteDir.concat('/static/imageData.json');
46-
fs.writeFileSync(outputPath, JSON.stringify(imageData, null, 2));
44+
for (let i = 0; i < masks.length; i++) {
45+
const mask = masks[i];
46+
const maskDataUrl = defaultMasks[i];
47+
const maskTitle = maskDataUrl.value.slice(
48+
maskDataUrl.value.lastIndexOf('/') + 1,
49+
);
50+
write(`./static${staticDir}masks/${maskTitle}`, mask, {
51+
recursive: true,
52+
});
53+
imageData.masks.push({
54+
type: 'url',
55+
imageType: 'mask',
56+
label: `${maskDataUrl.label} (${mask.width}x${mask.height})`,
57+
value: `${staticDir}masks/${maskTitle}`,
58+
});
59+
}
60+
61+
const outputPath = `./static${staticDir}imageData.json`;
62+
63+
fs.writeFileSync(outputPath, JSON.stringify(imageData, null, 2));
64+
} catch (error) {
65+
throw new Error(`Error in imageLoader: ${error.message}`);
66+
}
67+
// Fetch all images in parallel
4768

48-
console.log('Image data saved to:', outputPath);
4969
return imageData;
5070
}
51-
52-
module.exports = { imageLoader };
Lines changed: 4 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,7 @@
1+
import dataset from '../../../../static/demoImages/imageData.json';
12
import type { UrlOption } from '../importImage/importImageContext';
23

3-
const standardUrl = 'https://demo-dataset.image-js.org/standard';
4-
const standardMaskUrl = 'https://demo-dataset.image-js.org/standard_mask';
5-
const morphologyMaskUrl = 'https://demo-dataset.image-js.org/morphology_mask';
4+
const defaultImages = dataset.images as UrlOption[];
5+
const defaultMasks = dataset.masks as UrlOption[];
66

7-
export const defaultImages: UrlOption[] = [
8-
{
9-
type: 'url',
10-
value: `${standardUrl}/jellybeans.png`,
11-
label: 'Jelly beans ',
12-
imageType: 'image',
13-
},
14-
{
15-
type: 'url',
16-
value: `${standardUrl}/female.png`,
17-
label: 'Female',
18-
imageType: 'image',
19-
},
20-
{
21-
type: 'url',
22-
value: `${standardUrl}/femaleBellLabs.png`,
23-
label: 'Female from Bell Labs ',
24-
imageType: 'image',
25-
},
26-
{
27-
type: 'url',
28-
value: `${standardUrl}/house.png`,
29-
label: 'House',
30-
imageType: 'image',
31-
},
32-
{
33-
type: 'url',
34-
value: `${standardUrl}/mandrill.png`,
35-
label: 'Mandrill',
36-
imageType: 'image',
37-
},
38-
{
39-
type: 'url',
40-
value: `${standardUrl}/peppers.png`,
41-
label: 'Peppers',
42-
imageType: 'image',
43-
},
44-
45-
{
46-
type: 'url',
47-
value: `${standardUrl}/barbara.jpg`,
48-
label: 'Barbara',
49-
imageType: 'image',
50-
},
51-
52-
{
53-
type: 'url',
54-
value: `${standardUrl}/boat.png`,
55-
label: 'Standard boat',
56-
imageType: 'image',
57-
},
58-
{
59-
type: 'url',
60-
value: `${standardUrl}/male.png`,
61-
label: 'Male',
62-
imageType: 'image',
63-
},
64-
{
65-
type: 'url',
66-
value: `${standardUrl}/airport.png`,
67-
label: 'Airport',
68-
imageType: 'image',
69-
},
70-
];
71-
72-
export const defaultMasks: UrlOption[] = [
73-
{
74-
type: 'url',
75-
value: `${morphologyMaskUrl}/circles.png`,
76-
label: 'Circles',
77-
imageType: 'mask',
78-
},
79-
{
80-
type: 'url',
81-
value: `${morphologyMaskUrl}/shapes.png`,
82-
label: 'Shapes',
83-
imageType: 'mask',
84-
},
85-
{
86-
type: 'url',
87-
value: `${morphologyMaskUrl}/star.png`,
88-
label: 'Star',
89-
imageType: 'mask',
90-
},
91-
{
92-
type: 'url',
93-
value: `${standardMaskUrl}/house.png`,
94-
label: 'House',
95-
imageType: 'mask',
96-
},
97-
{
98-
type: 'url',
99-
value: `${standardMaskUrl}/mandrill.png`,
100-
label: 'Mandrill',
101-
imageType: 'mask',
102-
},
103-
{
104-
type: 'url',
105-
value: `${standardMaskUrl}/peppers.png`,
106-
label: 'Peppers',
107-
imageType: 'mask',
108-
},
109-
110-
{
111-
type: 'url',
112-
value: `${standardMaskUrl}/barbara.png`,
113-
label: 'Barbara',
114-
imageType: 'mask',
115-
},
116-
{
117-
type: 'url',
118-
value: `${standardMaskUrl}/boat.png`,
119-
label: 'Standard boat)',
120-
imageType: 'mask',
121-
},
122-
123-
{
124-
type: 'url',
125-
value: `${standardMaskUrl}/male.png`,
126-
label: 'Male',
127-
imageType: 'mask',
128-
},
129-
];
7+
export { defaultImages, defaultMasks };

0 commit comments

Comments
 (0)