इमेज जनरेट करें

Gemini API, Gemini 2.0 Flash Experimental और Imagen 3 का इस्तेमाल करके इमेज जनरेट करने की सुविधा देता है. इस गाइड की मदद से, दोनों मॉडल का इस्तेमाल शुरू किया जा सकता है.

Gemini का इस्तेमाल करके इमेज जनरेट करना

Gemini 2.0 Flash Experimental, टेक्स्ट और इनलाइन इमेज को आउटपुट करने की सुविधा देता है. इसकी मदद से, Gemini का इस्तेमाल करके बातचीत के तौर पर इमेज में बदलाव किया जा सकता है या टेक्स्ट के साथ आउटपुट जनरेट किए जा सकते हैं. उदाहरण के लिए, एक ही बार में टेक्स्ट और इमेज के साथ ब्लॉग पोस्ट जनरेट करना. जनरेट की गई सभी इमेज में SynthID वॉटरमार्क शामिल होता है. साथ ही, Google AI Studio में मौजूद इमेज में दिखने वाला वॉटरमार्क भी शामिल होता है.

नीचे दिए गए उदाहरण में, टेक्स्ट और इमेज वाला आउटपुट जनरेट करने के लिए, Gemini 2.0 का इस्तेमाल करने का तरीका बताया गया है:

PythonJavaScriptREST
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import base64

client = genai.Client()

contents = ('Hi, can you create a 3d rendered image of a pig '
            'with wings and a top hat flying over a happy '
            'futuristic scifi city with lots of greenery?')

response = client.models.generate_content(
    model="gemini-2.0-flash-exp-image-generation",
    contents=contents,
    config=types.GenerateContentConfig(
      response_modalities=['Text', 'Image']
    )
)

for part in response.candidates[0].content.parts:
  if part.text is not None:
    print(part.text)
  elif part.inline_data is not None:
    image = Image.open(BytesIO((part.inline_data.data)))
    image.save('gemini-native-image.png')
    image.show()
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

async function main() {

  const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });

  const contents =
    "Hi, can you create a 3d rendered image of a pig " +
    "with wings and a top hat flying over a happy " +
    "futuristic scifi city with lots of greenery?";

  // Set responseModalities to include "Image" so the model can generate  an image
  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash-exp-image-generation",
    contents: contents,
    config: {
      responseModalities: ["Text", "Image"],
    },
  });
  for (const part of response.candidates[0].content.parts) {
    // Based on the part type, either show the text or save the image
    if (part.text) {
      console.log(part.text);
    } else if (part.inlineData) {
      const imageData = part.inlineData.data;
      const buffer = Buffer.from(imageData, "base64");
      fs.writeFileSync("gemini-native-image.png", buffer);
      console.log("Image saved as gemini-native-image.png");
    }
  }
}

main();
curl -s -X POST \
  "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp-image-generation:generateContent?key=$GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{
      "parts": [
        {"text": "Hi, can you create a 3d rendered image of a pig with wings and a top hat flying over a happy futuristic scifi city with lots of greenery?"}
      ]
    }],
    "generationConfig":{"responseModalities":["Text","Image"]}
  }' \
  | grep -o '"data": "[^"]*"' \
  | cut -d'"' -f4 \
  | base64 --decode > gemini-native-image.png
एआई से जनरेट की गई, उड़ती हुई एक काल्पनिक सुअर की इमेज
एआई से जनरेट की गई, फ़ैंटासी वाली उड़ती हुई सूअर की इमेज

प्रॉम्प्ट और कॉन्टेक्स्ट के आधार पर, Gemini अलग-अलग मोड में कॉन्टेंट जनरेट करेगा. जैसे, टेक्स्ट से इमेज, टेक्स्ट से इमेज और टेक्स्ट वगैरह. यहां कुछ उदाहरण दिए गए हैं:

  • टेक्स्ट प्रॉम्प्ट से इमेज जनरेट करने की प्रोसेस
    • प्रॉम्प्ट का उदाहरण: "आइफ़िल टावर की ऐसी इमेज जनरेट करें जिसमें बैकग्राउंड में आतिशबाजी हो."
  • टेक्स्ट को इमेज और टेक्स्ट में बदलना (इंटरलीव किया गया)
    • प्रॉम्प्ट का उदाहरण: "पेल्हा की इमेज वाली रेसिपी जनरेट करें."
  • इमेज और टेक्स्ट को इमेज और टेक्स्ट में बदलना (इंटरलीव किया गया)
    • प्रॉम्प्ट का उदाहरण: (फ़र्निश किए गए कमरे की इमेज के साथ) "मेरे कमरे में किस रंग के सोफ़े अच्छे लगेंगे? क्या इमेज को अपडेट किया जा सकता है?"
  • इमेज में बदलाव करना (टेक्स्ट और इमेज को इमेज में बदलना)
    • प्रॉम्प्ट का उदाहरण: "इस इमेज में बदलाव करके, उसे कार्टून जैसा बनाएं"
    • प्रॉम्प्ट का उदाहरण: [बिल्ली की इमेज] + [तकिए की इमेज] + "इस तकिए पर मेरी बिल्ली की क्रॉस सिलाई वाली इमेज बनाओ."
  • एक से ज़्यादा बार इमेज में बदलाव करना (चैट)
    • प्रॉम्प्ट के उदाहरण: [नीली कार की इमेज अपलोड करें.] "इस कार को कन्वर्टिबल में बदलें." "अब रंग को पीले में बदलें."

Gemini की मदद से इमेज में बदलाव करना

इमेज में बदलाव करने के लिए, इनपुट के तौर पर कोई इमेज जोड़ें. नीचे दिए गए उदाहरण में, Base64 कोड में बदली गई इमेज अपलोड करने का तरीका बताया गया है. एक से ज़्यादा इमेज और बड़े पेलोड के लिए, इमेज इनपुट सेक्शन देखें.

PythonJavaScriptREST
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO

import PIL.Image

image = PIL.Image.open('/path/to/image.png')

client = genai.Client()

text_input = ('Hi, This is a picture of me.'
            'Can you add a llama next to me?',)

response = client.models.generate_content(
    model="gemini-2.0-flash-exp-image-generation",
    contents=[text_input, image],
    config=types.GenerateContentConfig(
      response_modalities=['Text', 'Image']
    )
)

for part in response.candidates[0].content.parts:
  if part.text is not None:
    print(part.text)
  elif part.inline_data is not None:
    image = Image.open(BytesIO(part.inline_data.data))
    image.show()
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

async function main() {

  const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });

  // Load the image from the local file system
  const imagePath = "path/to/image.png";
  const imageData = fs.readFileSync(imagePath);
  const base64Image = imageData.toString("base64");

  // Prepare the content parts
  const contents = [
    { text: "Can you add a llama next to the image?" },
    {
      inlineData: {
        mimeType: "image/png",
        data: base64Image,
      },
    },
  ];

  // Set responseModalities to include "Image" so the model can generate an image
  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash-exp-image-generation",
    contents: contents,
    config: {
      responseModalities: ["Text", "Image"],
    },
  });
  for (const part of response.candidates[0].content.parts) {
    // Based on the part type, either show the text or save the image
    if (part.text) {
      console.log(part.text);
    } else if (part.inlineData) {
      const imageData = part.inlineData.data;
      const buffer = Buffer.from(imageData, "base64");
      fs.writeFileSync("gemini-native-image.png", buffer);
      console.log("Image saved as gemini-native-image.png");
    }
  }
}

main();
IMG_PATH=/path/to/your/image1.jpeg

if [[ "$(base64 --version 2>&1)" = *"FreeBSD"* ]]; then
  B64FLAGS="--input"
else
  B64FLAGS="-w0"
fi

IMG_BASE64=$(base64 "$B64FLAGS" "$IMG_PATH" 2>&1)

curl -X POST \
  "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp-image-generation:generateContent?key=$GEMINI_API_KEY" \
    -H 'Content-Type: application/json' \
    -d "{
      \"contents\": [{
        \"parts\":[
            {\"text\": \"'Hi, This is a picture of me. Can you add a llama next to me\"},
            {
              \"inline_data\": {
                \"mime_type\":\"image/jpeg\",
                \"data\": \"$IMG_BASE64\"
              }
            }
        ]
      }],
      \"generationConfig\": {\"responseModalities\": [\"Text\", \"Image\"]}
    }"  \
  | grep -o '"data": "[^"]*"' \
  | cut -d'"' -f4 \
  | base64 --decode > gemini-edited-image.png

सीमाएं

  • सबसे अच्छी परफ़ॉर्मेंस के लिए, इन भाषाओं का इस्तेमाल करें: EN, es-MX, ja-JP, zh-CN, hi-IN.
  • इमेज जनरेशन में ऑडियो या वीडियो इनपुट का इस्तेमाल नहीं किया जा सकता.
  • ऐसा हो सकता है कि इमेज जनरेट करने की सुविधा हमेशा ट्रिगर न हो:
    • मॉडल सिर्फ़ टेक्स्ट आउटपुट कर सकता है. इमेज के आउटपुट के लिए साफ़ तौर पर पूछें. उदाहरण के लिए, "इमेज जनरेट करें", "काम करते समय इमेज दें", "इमेज अपडेट करें".
    • ऐसा हो सकता है कि मॉडल, डेटा जनरेट करने के दौरान ही रुक जाए. फिर से कोशिश करें या कोई दूसरा प्रॉम्प्ट आज़माएं.
  • किसी इमेज के लिए टेक्स्ट जनरेट करते समय, Gemini सबसे बेहतर तरीके से काम करता है. इसके लिए, पहले टेक्स्ट जनरेट करें और फिर उस टेक्स्ट के साथ इमेज जनरेट करने के लिए कहें.

कोई मॉडल चुनना

इमेज जनरेट करने के लिए, आपको किस मॉडल का इस्तेमाल करना चाहिए? यह इस बात पर निर्भर करता है कि आपको किस तरह का डेटा चाहिए.

Gemini 2.0, संदर्भ के हिसाब से काम की इमेज बनाने, टेक्स्ट और इमेज को ब्लेंड करने, दुनिया के बारे में जानकारी शामिल करने, और इमेज के बारे में रीज़निंग करने के लिए सबसे अच्छा है. इसका इस्तेमाल करके, लंबे टेक्स्ट सिलसिलों में एम्बेड किए गए सटीक और काम के विज़ुअल बनाए जा सकते हैं. बातचीत के दौरान, इमेज में बदलाव भी किया जा सकता है. इसके लिए, सामान्य भाषा का इस्तेमाल करें और बातचीत के दौरान संदर्भ बनाए रखें.

अगर इमेज की क्वालिटी आपकी सबसे ज़्यादा प्राथमिकता है, तो Imagen 3 एक बेहतर विकल्प है. Imagen 3, फ़ोटोरियलिज़्म, कला से जुड़ी जानकारी, और इंप्रेशनिस्ट या ऐनिमेशन जैसी खास कलात्मक शैलियों में बेहतरीन परफ़ॉर्म करता है. Imagen 3, इमेज में बदलाव करने के खास कामों के लिए भी एक अच्छा विकल्प है. जैसे, प्रॉडक्ट के बैकग्राउंड को अपडेट करना, इमेज को बड़ा करना, और विज़ुअल में ब्रैंडिंग और स्टाइल जोड़ना. लोगो या ब्रैंड वाले अन्य प्रॉडक्ट डिज़ाइन बनाने के लिए, Imagen 3 का इस्तेमाल किया जा सकता है.

Imagen 3 का इस्तेमाल करके इमेज जनरेट करना

Gemini API, Imagen 3 का ऐक्सेस देता है. यह Google का टेक्स्ट से इमेज जनरेट करने वाला सबसे बेहतर मॉडल है. इसमें कई नई और बेहतर सुविधाएं हैं. Imagen 3 ये काम कर सकता है:

  • पिछले मॉडल की तुलना में, बेहतर जानकारी, बेहतर रोशनी, और कम ध्यान भटकाने वाले आर्टफ़ैक्ट वाली इमेज जनरेट करना
  • आम भाषा में लिखे गए प्रॉम्प्ट को समझना
  • अलग-अलग फ़ॉर्मैट और स्टाइल में इमेज जनरेट करना
  • पिछले मॉडल की तुलना में, टेक्स्ट को ज़्यादा असरदार तरीके से रेंडर करना
PythonJavaScriptREST
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO

client = genai.Client(api_key='GEMINI_API_KEY')

response = client.models.generate_images(
    model='imagen-3.0-generate-002',
    prompt='Robot holding a red skateboard',
    config=types.GenerateImagesConfig(
        number_of_images= 4,
    )
)
for generated_image in response.generated_images:
  image = Image.open(BytesIO(generated_image.image.image_bytes))
  image.show()
import { GoogleGenAI } from "@google/genai";
import * as fs from "node:fs";

async function main() {

  const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });

  const response = await ai.models.generateImages({
    model: 'imagen-3.0-generate-002',
    prompt: 'Robot holding a red skateboard',
    config: {
      numberOfImages: 4,
    },
  });

  let idx = 1;
  for (const generatedImage of response.generatedImages) {
    let imgBytes = generatedImage.image.imageBytes;
    const buffer = Buffer.from(imgBytes, "base64");
    fs.writeFileSync(`imagen-${idx}.png`, buffer);
    idx++;
  }
}

main();
curl -X POST \
    "https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate-002:predict?key=GEMINI_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
        "instances": [
          {
            "prompt": "Robot holding a red skateboard"
          }
        ],
        "parameters": {
          "sampleCount": 4
        }
      }'
रसोई में दो फ़ज़ी बन्नी की एआई से जनरेट की गई इमेज
किचन में मौजूद दो फ़ज़ी बन्नी की जनरेटिव एआई से बनाई गई इमेज

फ़िलहाल, Imagen सिर्फ़ अंग्रेज़ी में प्रॉम्प्ट और इन पैरामीटर के साथ काम करता है:

Imagen मॉडल के पैरामीटर

(नाम रखने के तरीके, प्रोग्रामिंग भाषा के हिसाब से अलग-अलग होते हैं.)

  • numberOfImages: जनरेट की जाने वाली इमेज की संख्या, 1 से 4 के बीच होनी चाहिए. डिफ़ॉल्ट रूप से, यह 4 पर सेट होता है.
  • aspectRatio: जनरेट की गई इमेज का आसपेक्ट रेशियो बदलता है. "1:1", "3:4", "4:3", "9:16", और "16:9" वैल्यू इस्तेमाल की जा सकती हैं. डिफ़ॉल्ट रूप से, यह "1:1" पर सेट होता है.
  • personGeneration: मॉडल को लोगों की इमेज जनरेट करने की अनुमति दें. ये वैल्यू इस्तेमाल की जा सकती हैं:
    • "DONT_ALLOW": लोगों की इमेज जनरेट होने से रोकना.
    • "ALLOW_ADULT": सिर्फ़ वयस्कों की इमेज जनरेट करें, बच्चों की नहीं. यह डिफ़ॉल्ट विकल्प है.

आगे क्या करना है