Generative AI with Node.js in Hindi #17 Read & Understand Image using Google Gen AI

Read and Understand Image with GenAI


Steps :-

  1. Read Image in node js with base 64
  2. Pass data to google gen ai
  3. Pass Prompt to read image


Code:-


import { GoogleGenAI } from '@google/genai'


import dotenv from 'dotenv'
import { readFileSync } from 'fs'
dotenv.config();
const GoogleAI = new GoogleGenAI({ apiKey: process.env.geminiKey });

async function main() {

const base64Img = readFileSync("img.png", {
encoding: "base64"
})

const response = await GoogleAI.models.generateContent({
model: "gemini-2.5-flash",
contents: [
{
inlineData: {
mimeType: "image/png",
data: base64Img
},
},
// {text:"read text from this image"}
{text:"tell me color combination of this image"}

]
})
console.log(response.text);
}

main();