Generative AI with Node.js in Hindi #9 Generate Images and Photos with DALL·E 2, DALL·E 3
Generate Image with OpenAI in node in App
Points and Steps:-
- Use OpenAI feature for generate image
- Save Image in local system
- Test Image is working properly or not
- Interview Questions
- Code and Notes link
Code:-
import OpenAI from "openai";
import dotenv from 'dotenv'
import {writeFileSync} from 'fs'
dotenv.config();
const client = new OpenAI({apiKey:process.env.openAI_Key})
async function main(){
const response = await client.images.generate({
model:"dall-e-2",
prompt:"generate image of a with blonde hair",
size:"512x512",
response_format:"b64_json",
n:1
})
console.log(response);
const rawImage = response.data[0].b64_json;
const path ="./generatedImg.png"
const butter = Buffer.from(rawImage,'base64')
writeFileSync(path,butter);
console.log("image is saved and path is "+ path);
}
main()