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:-

  1. Use OpenAI feature for generate image
  2. Save Image in local system
  3. Test Image is working properly or not
  4. Interview Questions
  5. 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()