-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-generator.js
35 lines (30 loc) · 1.17 KB
/
image-generator.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
const createImageGenerator = ({ openai, path, fs, axios }) => {
const generateImage = async (imageData, timeStamp, size) => {
const imageSize =
size === 'small' ? '256x256' : size === 'medium' ? '512x512' : '1024x1024';
// const dallePrompt = "Generate an image of: " + prompt;
try {
const response = await openai.images.createVariation({
image: fs.createReadStream(imageData),
n: 1,
size: imageSize,
});
const imageUrl = response.data[0].url;
const imagePath = path.resolve("./public/" + `${timeStamp}_dalle.png`);
// Download and save the image
const imageResponse = await axios.get(imageUrl, { responseType: 'arraybuffer' });
fs.writeFileSync(imagePath, imageResponse.data);
console.log("Generated image saved to:", imagePath);
return {
imageUrl,
savedImagePath: `${imagePath}/${timeStamp}_dalle.png` // Return the absolute path
};
} catch (error) {
console.error("Error generating or saving image:", error);
throw error;
}
};
// Return an object with the generateImage function
return { generateImage };
};
module.exports = createImageGenerator;