Gemini API, Gemini 2.0 Flash Experimental का इस्तेमाल करके और Imagen 3 का इस्तेमाल करके इमेज जनरेट करने की सुविधा देता है. इस गाइड की मदद से, दोनों मॉडल का इस्तेमाल शुरू किया जा सकता है.
Gemini का इस्तेमाल करके इमेज जनरेट करना
Gemini 2.0 Flash Experimental, टेक्स्ट और इनलाइन इमेज को आउटपुट करने की सुविधा देता है. इसकी मदद से, Gemini का इस्तेमाल करके बातचीत के तौर पर इमेज में बदलाव किया जा सकता है या टेक्स्ट के साथ आउटपुट जनरेट किए जा सकते हैं. उदाहरण के लिए, एक ही बार में टेक्स्ट और इमेज के साथ ब्लॉग पोस्ट जनरेट करना. जनरेट की गई सभी इमेज में SynthID वॉटरमार्क शामिल होता है. साथ ही, Google AI Studio में मौजूद इमेज में दिखने वाला वॉटरमार्क भी शामिल होता है.
नीचे दिए गए उदाहरण में, टेक्स्ट और इमेज वाला आउटपुट जनरेट करने के लिए, Gemini 2.0 का इस्तेमाल करने का तरीका बताया गया है:
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()
const { GoogleGenerativeAI } = require("@google/generative-ai");
const fs = require("fs");
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
async function generateImage() {
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 model = genAI.getGenerativeModel({
model: "gemini-2.0-flash-exp-image-generation",
generationConfig: {
responseModalities: ['Text', 'Image']
},
});
try {
const response = await model.generateContent(contents);
for (const part of response.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');
}
}
} catch (error) {
console.error("Error generating content:", error);
}
}
generateImage();
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 कोड में बदली गई इमेज अपलोड करने का तरीका बताया गया है. एक से ज़्यादा इमेज और बड़े पेलोड के लिए, इमेज इनपुट सेक्शन देखें.
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()
const { GoogleGenerativeAI } = require("@google/generative-ai");
const fs = require("fs");
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
async function generateImage() {
// 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: "Hi, This is a picture of me. Can you add a llama next to me?" },
{
inlineData: {
mimeType: 'image/png',
data: base64Image
}
}
];
// Set responseModalities to include "Image" so the model can generate an image
const model = genAI.getGenerativeModel({
model: "gemini-2.0-flash-exp-image-generation",
generationConfig: {
responseModalities: ['Text', 'Image']
},
});
try {
const response = await model.generateContent(contents);
for (const part of response.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');
}
}
} catch (error) {
console.error("Error generating content:", error);
}
}
generateImage();
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 ये काम कर सकता है:
- पिछले मॉडल की तुलना में, बेहतर जानकारी, बेहतर रोशनी, और कम ध्यान भटकाने वाले आर्टफ़ैक्ट वाली इमेज जनरेट करना
- आम भाषा में लिखे गए प्रॉम्प्ट को समझना
- अलग-अलग फ़ॉर्मैट और स्टाइल में इमेज जनरेट करना
- पिछले मॉडल की तुलना में, टेक्स्ट को ज़्यादा असरदार तरीके से रेंडर करना
Imagen का उदाहरण
इस सेक्शन में, Imagen मॉडल को इंस्टैंशिएट करने और इमेज जनरेट करने का तरीका बताया गया है.
Google Gen AI SDK इंस्टॉल करने के बाद, इमेज जनरेट करने के लिए इस कोड का इस्तेमाल किया जा सकता है:
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='Fuzzy bunnies in my kitchen',
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()
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": "Fuzzy bunnies in my kitchen"
}
],
"parameters": {
"sampleCount": 4
}
}'
कोड सैंपल से इस तरह की चार इमेज दिखनी चाहिए:
Gemini Cookbook में, Imagen नोटबुक का इस्तेमाल शुरू करने के बारे में जानकारी भी देखी जा सकती है.
Imagen मॉडल के पैरामीटर
generate_images()
के लिए ये पैरामीटर उपलब्ध हैं:
prompt
: इमेज के लिए टेक्स्ट प्रॉम्प्ट.number_of_images
: जनरेट की जाने वाली इमेज की संख्या, 1 से 4 के बीच होनी चाहिए. डिफ़ॉल्ट रूप से, यह संख्या 4 होती है.aspect_ratio
: जनरेट की गई इमेज का आसपेक्ट रेशियो बदलता है."1:1"
,"3:4"
,"4:3"
,"9:16"
, और"16:9"
वैल्यू इस्तेमाल की जा सकती हैं. डिफ़ॉल्ट रूप से, यह"1:1"
पर सेट होता है.safety_filter_level
: सुरक्षा फ़िल्टर करने के लिए, फ़िल्टर लेवल जोड़ता है. ये वैल्यू मान्य हैं:"BLOCK_LOW_AND_ABOVE"
: जब संभावना का स्कोर या गंभीरता का स्कोरLOW
,MEDIUM
याHIGH
हो, तो ब्लॉक करें."BLOCK_MEDIUM_AND_ABOVE"
: जब संभावना का स्कोर या गंभीरता का स्कोरMEDIUM
याHIGH
हो, तब ब्लॉक करें."BLOCK_ONLY_HIGH"
: जब संभावना का स्कोर या गंभीरता का स्कोरHIGH
हो, तो ब्लॉक करें.
person_generation
: मॉडल को लोगों की इमेज जनरेट करने की अनुमति दें. ये वैल्यू इस्तेमाल की जा सकती हैं:"DONT_ALLOW"
: लोगों की इमेज जनरेट होने से रोकना."ALLOW_ADULT"
: सिर्फ़ वयस्कों की इमेज जनरेट करें, बच्चों की नहीं. यह डिफ़ॉल्ट विकल्प है.
जनरेट की गई इमेज में, दिखने वाला डिजिटल SynthID वॉटरमार्क हमेशा जोड़ा जाता है.
टेक्स्ट प्रॉम्प्ट की भाषा
इनपुट टेक्स्ट प्रॉम्प्ट की ये भाषाएं काम करती हैं:
- अंग्रेज़ी (
en
)
आगे क्या करना है
- Imagen के लिए प्रॉम्प्ट लिखने के बारे में ज़्यादा जानने के लिए, Imagen के प्रॉम्प्ट के लिए गाइड देखें.
- Gemini 2.0 मॉडल के बारे में ज़्यादा जानने के लिए, Gemini मॉडल और एक्सपेरिमेंट के तौर पर उपलब्ध मॉडल देखें.