Image API, Product Studio के सब-एपीआई का हिस्सा है. यह प्रॉडक्ट इमेज जनरेट करने और उन्हें ऑप्टिमाइज़ करने के लिए, Google Product Studio (GPS) API के जनरेटिव एआई टूल का इस्तेमाल करता है. इसका इस्तेमाल, ग्राहकों की दिलचस्पी बढ़ाने और कन्वर्ज़न को बेहतर बनाने के लिए किया जा सकता है.
एपीआई का इस्तेमाल करके क्या-क्या जनरेट किया जा सकता है?
- प्रॉडक्ट की जानकारी और टेक्स्ट प्रॉम्प्ट के आधार पर, एआई से जनरेट किए गए बैकग्राउंड वाली प्रॉडक्ट इमेज
- प्रॉडक्ट की ऐसी इमेज जिनमें ओरिजनल इमेज का बैकग्राउंड हटा दिया गया हो
- ऐसी प्रॉडक्ट इमेज जिनमें ओरिजनल इमेज का रिज़ॉल्यूशन बेहतर बनाया गया हो
इमेज का बैकग्राउंड जनरेट करना
GenerateProductImageBackground
तरीके से, एआई की मदद से प्रॉडक्ट की इमेज बनाई जा सकती हैं. इसके लिए, आपको प्रॉडक्ट की ओरिजनल इमेज और बैकग्राउंड के बारे में जानकारी देने वाला टेक्स्ट प्रॉम्प्ट इस्तेमाल करना होगा.
एपीआई इन फ़ॉर्मैट में डेटा स्वीकार करता है:
- प्रॉडक्ट इमेज: यह इनमें से कोई एक हो सकती है:
- प्रॉडक्ट की इमेज (जैसे,
{"image_uri": "https://my-store.com/img/1.png"}
) की ओर ले जाने वाला यूआरआई - इमेज की रॉ बाइट, जैसे कि
{"image_bytes": b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01'}
- प्रॉडक्ट की इमेज (जैसे,
- प्रॉम्प्ट: यह एक स्ट्रिंग होती है, जो जनरेट किए जाने वाले बैकग्राउंड के बारे में बताती है:
product_description
: प्रॉडक्ट का ब्यौरा.background_description
: मनचाहे बैकग्राउंड की जानकारी.
किसी इमेज का बैकग्राउंड हटाना
इस
RemoveProductImageBackground
तरीके से, आपकी ओरिजनल प्रॉडक्ट इमेज से बैकग्राउंड हटाकर इमेज जनरेट की जा सकती हैं.
एपीआई इन फ़ॉर्मैट में डेटा स्वीकार करता है:
- प्रॉडक्ट इमेज: यह इनमें से कोई एक हो सकती है:
- प्रॉडक्ट की इमेज दिखाने वाला यूआरआई, जैसे कि
{"image_uri": "https://my-store.com/img/1.png"}
- इमेज की रॉ बाइट, जैसे कि
{"image_bytes": b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01'}
- प्रॉडक्ट की इमेज दिखाने वाला यूआरआई, जैसे कि
- बैकग्राउंड का रंग: इमेज से बैकग्राउंड हटाने के बाद, बैकग्राउंड का रंग. यह रंग आरजीबी फ़ॉर्मैट में दिया जाता है. अगर कोई रंग तय नहीं किया गया है, तो बैकग्राउंड एक आरजीबीए 4-चैनल वाली पारदर्शी इमेज होगी.
किसी इमेज का रिज़ॉल्यूशन बढ़ाना
UpscaleProductImage
तरीके से, ऐसी इमेज जनरेट की जा सकती हैं जिनमें आपके प्रॉडक्ट की ओरिजनल इमेज का रिज़ॉल्यूशन बेहतर होता है.
एपीआई, प्रॉडक्ट इमेज स्वीकार करता है. यह इनमें से कोई एक हो सकता है:
- प्रॉडक्ट की इमेज दिखाने वाला यूआरआई, जैसे कि
{"image_uri": "https://my-store.com/img/1.png"}
- इमेज की रॉ बाइट, जैसे कि
{"image_bytes": "image_bytes"}
क्लाइंट लाइब्रेरी का इस्तेमाल करना
हमारा सुझाव है कि अनुरोध सबमिट करने के लिए, क्लाइंट लाइब्रेरी का इस्तेमाल करें. अगर आपकी भाषा के लिए, क्लाइंट लाइब्रेरी उपलब्ध नहीं है, तो हम आपको लाइब्रेरी फ़ाइलें सीधे तौर पर उपलब्ध करा सकते हैं. जैसे, ईमेल या शेयर किए गए ड्राइव फ़ोल्डर में. इससे आपको उन्हें इंस्टॉल करने में मदद मिलेगी. उदाहरण के लिए, अपने Maven प्रोजेक्ट में.
उदाहरण
यहां दिए गए उदाहरणों में, प्रॉडक्ट की इमेज जनरेट करने या उन्हें ऑप्टिमाइज़ करने के लिए, एपीआई का इस्तेमाल किया गया है.
किसी प्रॉडक्ट की इमेज के लिए बैकग्राउंड जनरेट करना
इस उदाहरण में, चुने गए बैकग्राउंड के साथ प्रॉडक्ट की इमेज जनरेट करने का तरीका दिखाया गया है.
अनुरोध
अनुरोध के मुख्य हिस्से में इमेज (यूआरआई या बाइट) और बैकग्राउंड जनरेट करने के लिए प्रॉम्प्ट शामिल होता है. यहां इमेज यूआरआई का इस्तेमाल करके, अनुरोध के स्ट्रक्चर का उदाहरण दिया गया है:
POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground
{
"input_image": {
"image_uri": "https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
},
"config": {
"product_description": "a jar",
"background_description": "sitting on a cracked stone surface surrounded by a cherry blossom tree and pink and white flowers in the background, high resolution, product photography, strong shadows and lights, creative"
}
}
इसके अलावा, रॉ इमेज बाइट का इस्तेमाल किया जा सकता है:
POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground
{
"input_image": {
"image_bytes": "b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff'"
},
"config": {
"product_description": "a jar",
"background_description": "sitting on a cracked stone surface surrounded by a cherry blossom tree and pink and white flowers in the background, high resolution, product photography, strong shadows and lights, creative"
}
}
जवाब
आपको इस तरह का जवाब मिल सकता है:
{
"generated_image": {
"image_bytes": "b'\x7d\x1b\x8f\x04\x5c\x9e\x23\xf0\x6a\xd1\x4b\x87\x0c\x3e\xa9\x21\xb5'",
"generation_time": "2025-05-13T00:06:54.754828Z"
},
}
अगर return_image_uri
को output_config,
में true
पर सेट किया जाता है, तो यह इस तरह दिखेगा:
{
"generated_image": {
"uri": "https://lh3.googleusercontent.com/gps-generations/AI9LgEvIFfK0YaLKvwvIBc9ItJtJGFE6hi25o6-N3Fz6YTw57pFghfIVlCGve7eyJcQkhoAqGAf8NDzW70qjwGOCuHhiouqF2nqxBwCcfDxZPkYRl3h7CIjyev9asJw7x-0g0V_xvurfowZJ0moXu98kmV01A3Thv2ZRlTAiOPYuEb8wnY7EeyD9l48lDdwJgZ4jqxtTgrRbwvpno4NICj9q830RzATiBHuJ_WUFQJiHOMAqbGyrQIWQTQ",
"generation_time": "2025-05-13T00:06:54.754828Z"
}
}
कोड सैंपल
यहां एक कोड सैंपल दिया गया है. इसमें प्रॉडक्ट की इमेज के बैकग्राउंड को जनरेट करने का तरीका बताया गया है:
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.productstudio.v1alpha.GenerateImageBackgroundConfig;
import com.google.shopping.merchant.productstudio.v1alpha.GenerateProductImageBackgroundRequest;
import com.google.shopping.merchant.productstudio.v1alpha.GenerateProductImageBackgroundResponse;
import com.google.shopping.merchant.productstudio.v1alpha.ImageServiceClient;
import com.google.shopping.merchant.productstudio.v1alpha.ImageServiceSettings;
import com.google.shopping.merchant.productstudio.v1alpha.InputImage;
import com.google.shopping.merchant.productstudio.v1alpha.OutputImageConfig;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to create product images with generated backgrounds. */
public class GenerateProductImageBackgroundSample {
private static String getName(String accountId) {
return String.format("accounts/%s", accountId);
}
public static void generateProductImageBackground(Config config, String imageUri)
throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
ImageServiceSettings imageServiceSettings =
ImageServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
String name = getName(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (ImageServiceClient imageServiceClient = ImageServiceClient.create(imageServiceSettings)) {
OutputImageConfig outputImageConfig =
// Set this field to false to return the image bytes in the response instead.
OutputImageConfig.newBuilder().setReturnImageUri(true).build();
InputImage inputImage =
InputImage.newBuilder()
// You can also use image bytes here instead of a URI.
.setImageUri(imageUri)
.build();
GenerateImageBackgroundConfig generateImageBackgroundConfig =
GenerateImageBackgroundConfig.newBuilder()
.setProductDescription("a jar")
.setBackgroundDescription(
"sitting on a cracked stone surface surrounded by a cherry blossom tree and pink"
+ " and white flowers in the background, high resolution, product"
+ " photography, strong shadows and lights, creative")
.build();
GenerateProductImageBackgroundRequest request =
GenerateProductImageBackgroundRequest.newBuilder()
.setName(name)
.setOutputConfig(outputImageConfig)
.setInputImage(inputImage)
.setConfig(generateImageBackgroundConfig)
.build();
System.out.println("Sending GenerateProductImageBackground request: " + name);
GenerateProductImageBackgroundResponse response =
imageServiceClient.generateProductImageBackground(request);
System.out.println("Generated product image background response below:");
System.out.println(response);
} catch (Exception e) {
System.out.println("An error has occurred: ");
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// Replace with your image URI.
String imageUri =
"https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg";
generateProductImageBackground(config, imageUri);
}
}
Python
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_productstudio_v1alpha import GenerateImageBackgroundConfig
from google.shopping.merchant_productstudio_v1alpha import GenerateProductImageBackgroundRequest
from google.shopping.merchant_productstudio_v1alpha import ImageServiceClient
from google.shopping.merchant_productstudio_v1alpha import InputImage
from google.shopping.merchant_productstudio_v1alpha import OutputImageConfig
# Gets the merchant account ID from the user's configuration.
_ACCOUNT = configuration.Configuration().read_merchant_info()
# The name of the account to which the request is sent.
# Format: accounts/{account}
_NAME = f"accounts/{_ACCOUNT}"
def generate_product_image_background(image_uri: str) -> None:
"""Generates a product image with a custom background.
Args:
image_uri: The URI of the input image.
"""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = ImageServiceClient(credentials=credentials)
# Creates the output config.
# Set `return_image_uri` to False to return the image bytes in the response.
output_config = OutputImageConfig(return_image_uri=True)
# Creates the input image.
# You can also use image bytes here instead of a URI.
input_image = InputImage(image_uri=image_uri)
# Creates the generate image background config.
generate_image_background_config = GenerateImageBackgroundConfig(
product_description="a jar",
background_description=(
"sitting on a cracked stone surface surrounded by a cherry blossom"
" tree and pink and white flowers in the background, high"
" resolution, product photography, strong shadows and lights,"
" creative"
),
)
# Creates the request.
request = GenerateProductImageBackgroundRequest(
name=_NAME,
output_config=output_config,
input_image=input_image,
config=generate_image_background_config,
)
# Makes the request and catches and prints any error messages.
try:
print(f"Sending GenerateProductImageBackground request: {_NAME}")
response = client.generate_product_image_background(request=request)
print("Generated product image background response below:")
print(response)
except RuntimeError as e:
print("Request failed.")
print(e)
if __name__ == "__main__":
# The URI of the image to use as the base for the generation.
# Replace with your image URI.
_IMAGE_URI = (
"https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
)
generate_product_image_background(_IMAGE_URI)
प्रॉडक्ट की इमेज से बैकग्राउंड हटाना
इस उदाहरण में, किसी प्रॉडक्ट की इमेज से बैकग्राउंड हटाने का तरीका दिखाया गया है.
अनुरोध
अनुरोध के मुख्य हिस्से में इमेज (यूआरआई या बाइट) शामिल होती है. इसके अलावा, बैकग्राउंड को बदलने के लिए रंग भी शामिल किया जा सकता है.
ध्यान दें:
- आरजीबी फ़ॉर्मैट में रंग की वैल्यू [0-255] की रेंज में डालें.
- अगर बैकग्राउंड का रंग नहीं चुना जाता है, तो यह आरजीबीए 4-चैनल वाली पारदर्शी इमेज होगी.
यहां इमेज यूआरआई का इस्तेमाल करके अनुरोध के स्ट्रक्चर का उदाहरण दिया गया है. इसमें बैकग्राउंड को हरा (0, 255, 0) पर सेट किया गया है:
POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:removeProductImageBackground
{
"input_image": {
"image_uri": "https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
},
"config": {
"background_color": {
"red": 0,
"green": 255,
"blue": 0
}
}
}
इसके अलावा, रॉ इमेज बाइट का इस्तेमाल करके भी यही काम किया जा सकता है:
POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:removeProductImageBackground
{
"input_image": {
"image_bytes": "b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff'"
},
"config": {
"background_color": {
"red": 0,
"green": 255,
"blue": 0
}
}
}
जवाब
आपको इस तरह का जवाब मिल सकता है:
{
"generated_image": {
"image_bytes": "b'\x7d\x1b\x8f\x04\x5c\x9e\x23\xf0\x6a\xd1\x4b\x87\x0c\x3e\xa9\x21\xb5'",
"generation_time": "2025-05-13T00:06:54.754828Z"
},
}
इसके अलावा, अगर return_image_uri
को output_config
में true
पर सेट किया गया है, तो यह इस तरह दिखेगा:
{
"generated_image": {
"uri": "https://lh3.googleusercontent.com/gps-generations/AI9LgEsYi83dcMKVV44i3vHDHzz_PJLuPyJHh9iX7IrsZ0ZIIRIS6SB4ovBmr0L9lZ1VNacZ5duwhcJ8Q8mXMYaIEArf2ieC2OuKEhRzVb-UxlkAUpitEpS5-M3Y3Ch3TGhQTZmBJXlsHLTuf0XcPwNHoNxJlgF0v_mQ6DIlPugZKC_WiYN2DNUkJivfr4WvifhMn5NaMKpDwpH4Acj-bdGNQ77M-8sj4SyEu3sHCF8ZtnMvF5DbGIYOJQ"
}
}
कोड सैंपल
यहां कोड का एक उदाहरण दिया गया है. इसमें बताया गया है कि किसी प्रॉडक्ट की इमेज का बैकग्राउंड कैसे हटाया जाता है:
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.productstudio.v1alpha.ImageServiceClient;
import com.google.shopping.merchant.productstudio.v1alpha.ImageServiceSettings;
import com.google.shopping.merchant.productstudio.v1alpha.InputImage;
import com.google.shopping.merchant.productstudio.v1alpha.OutputImageConfig;
import com.google.shopping.merchant.productstudio.v1alpha.RemoveImageBackgroundConfig;
import com.google.shopping.merchant.productstudio.v1alpha.RemoveProductImageBackgroundRequest;
import com.google.shopping.merchant.productstudio.v1alpha.RemoveProductImageBackgroundResponse;
import com.google.shopping.merchant.productstudio.v1alpha.RgbColor;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to create product images with the background removed. */
public class RemoveProductImageBackgroundSample {
private static String getName(String accountId) {
return String.format("accounts/%s", accountId);
}
public static void removeProductImageBackground(Config config, String imageUri) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
ImageServiceSettings imageServiceSettings =
ImageServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
String name = getName(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (ImageServiceClient imageServiceClient = ImageServiceClient.create(imageServiceSettings)) {
OutputImageConfig outputImageConfig =
// Set this field to false to return the image bytes in the response instead.
OutputImageConfig.newBuilder().setReturnImageUri(true).build();
InputImage inputImage =
InputImage.newBuilder()
// You can also use image bytes here instead of a URI.
.setImageUri(imageUri)
.build();
RemoveImageBackgroundConfig removeImageBackgroundConfig =
RemoveImageBackgroundConfig.newBuilder()
// Setting the background color to green. Don't set this field if you want the image
// to have a RGBA 4-channel transparent image as the background.
.setBackgroundColor(RgbColor.newBuilder().setRed(0).setGreen(255).setBlue(0))
.build();
RemoveProductImageBackgroundRequest request =
RemoveProductImageBackgroundRequest.newBuilder()
.setName(name)
.setOutputConfig(outputImageConfig)
.setInputImage(inputImage)
.setConfig(removeImageBackgroundConfig)
.build();
System.out.println("Sending RemoveProductImageBackground request: " + name);
RemoveProductImageBackgroundResponse response =
imageServiceClient.removeProductImageBackground(request);
System.out.println("Removed product image background response below:");
System.out.println(response);
} catch (Exception e) {
System.out.println("An error has occurred: ");
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// Replace with your image URI.
String imageUri =
"https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg";
removeProductImageBackground(config, imageUri);
}
}
Python
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_productstudio_v1alpha import ImageServiceClient
from google.shopping.merchant_productstudio_v1alpha import InputImage
from google.shopping.merchant_productstudio_v1alpha import OutputImageConfig
from google.shopping.merchant_productstudio_v1alpha import RemoveImageBackgroundConfig
from google.shopping.merchant_productstudio_v1alpha import RemoveProductImageBackgroundRequest
from google.shopping.merchant_productstudio_v1alpha import RgbColor
# Gets the merchant account ID from the user's configuration.
_ACCOUNT = configuration.Configuration().read_merchant_info()
# The name of the account to which the request is sent.
# Format: accounts/{account}
_NAME = f"accounts/{_ACCOUNT}"
def remove_product_image_background(image_uri: str) -> None:
"""Removes the background from a product image.
Args:
image_uri: The URI of the input image.
"""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = ImageServiceClient(credentials=credentials)
# Creates the output config.
# Set `return_image_uri` to False to return the image bytes in the response.
output_config = OutputImageConfig(return_image_uri=True)
# Creates the input image.
# You can also use image bytes here instead of a URI.
input_image = InputImage(image_uri=image_uri)
# Creates the remove image background config.
# Setting the background color to green. Don't set this field if you want the
# image to have a RGBA 4-channel transparent image as the background.
remove_image_background_config = RemoveImageBackgroundConfig(
background_color=RgbColor(red=0, green=255, blue=0)
)
# Creates the request.
request = RemoveProductImageBackgroundRequest(
name=_NAME,
output_config=output_config,
input_image=input_image,
config=remove_image_background_config,
)
# Makes the request and catches and prints any error messages.
try:
print(f"Sending RemoveProductImageBackground request: {_NAME}")
response = client.remove_product_image_background(request=request)
print("Removed product image background response below:")
print(response)
except RuntimeError as e:
print("Request failed.")
print(e)
if __name__ == "__main__":
# The URI of the image to remove the background from.
# Replace with your image URI.
_IMAGE_URI = (
"https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
)
remove_product_image_background(_IMAGE_URI)
किसी प्रॉडक्ट इमेज का रिज़ॉल्यूशन बढ़ाना
इस उदाहरण में, प्रॉडक्ट की इमेज की क्वालिटी और साइज़ को बेहतर बनाने का तरीका दिखाया गया है.
अनुरोध
अनुरोध के मुख्य हिस्से में इमेज (यूआरआई या बाइट) शामिल होती है. यहां इमेज यूआरआई का इस्तेमाल करके, अनुरोध के स्ट्रक्चर का एक उदाहरण दिया गया है:
POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:upscaleProductImage
{
"input_image": {
"image_uri": "https://services.google.com/fh/files/misc/ring_image_400_600.jpg"
}
}
इसके अलावा, रॉ इमेज बाइट का इस्तेमाल किया जा सकता है:
POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:upscaleProductImage
{
"input_image": {
"image_bytes": "b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff'"
}
}
जवाब
आपको इस तरह का जवाब मिल सकता है
{
"generated_image": {
"image_bytes": "b'\x7d\x1b\x8f\x04\x5c\x9e\x23\xf0\x6a\xd1\x4b\x87\x0c\x3e\xa9\x21\xb5'",
"generation_time": "2025-05-13T00:06:54.754828Z"
},
}
अगर return_image_uri
को output_config,
में true
पर सेट किया जाता है, तो यह इस तरह दिखेगा:
{
"generated_image": {
"uri": "https://lh3.googleusercontent.com/gps-generations/AI9LgEvIFfK0YaLKvwvIBc9ItJtJGFE6hi25o6-N3Fz6YTw57pFghfIVlCGve7eyJcQkhoAqGAf8NDzW70qjwGOCuHhiouqF2nqxBwCcfDxZPkYRl3h7CIjyev9asJw7x-0g0V_xvurfowZJ0moXu98kmV01A3Thv2ZRlTAiOPYuEb8wnY7EeyD9l48lDdwJgZ4jqxtTgrRbwvpno4NICj9q830RzATiBHuJ_WUFQJiHOMAqbGyrQIWQTQ"
}
}
कोड सैंपल
यहां एक कोड सैंपल दिया गया है. इसमें बताया गया है कि किसी प्रॉडक्ट की इमेज को अपस्केल कैसे किया जाता है:
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.productstudio.v1alpha.ImageServiceClient;
import com.google.shopping.merchant.productstudio.v1alpha.ImageServiceSettings;
import com.google.shopping.merchant.productstudio.v1alpha.InputImage;
import com.google.shopping.merchant.productstudio.v1alpha.OutputImageConfig;
import com.google.shopping.merchant.productstudio.v1alpha.UpscaleProductImageRequest;
import com.google.shopping.merchant.productstudio.v1alpha.UpscaleProductImageResponse;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to create upscaled product images. */
public class UpscaleProductImageSample {
private static String getName(String accountId) {
return String.format("accounts/%s", accountId);
}
public static void upscaleProductImage(Config config, String imageUri) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
ImageServiceSettings imageServiceSettings =
ImageServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
String name = getName(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (ImageServiceClient imageServiceClient = ImageServiceClient.create(imageServiceSettings)) {
OutputImageConfig outputImageConfig =
// Set this field to false to return the image bytes in the response instead.
OutputImageConfig.newBuilder().setReturnImageUri(true).build();
InputImage inputImage =
InputImage.newBuilder()
// You can also use image bytes here instead of a URI.
.setImageUri(imageUri)
.build();
UpscaleProductImageRequest request =
UpscaleProductImageRequest.newBuilder()
.setName(name)
.setOutputConfig(outputImageConfig)
.setInputImage(inputImage)
.build();
System.out.println("Sending UpscaleProductImage request: " + name);
UpscaleProductImageResponse response = imageServiceClient.upscaleProductImage(request);
System.out.println("Upscaled product image response below:");
System.out.println(response);
} catch (Exception e) {
System.out.println("An error has occurred: ");
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// Replace with your image URI.
String imageUri = "https://services.google.com/fh/files/misc/ring_image_400_600.jpg";
upscaleProductImage(config, imageUri);
}
}
Python
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_productstudio_v1alpha import ImageServiceClient
from google.shopping.merchant_productstudio_v1alpha import InputImage
from google.shopping.merchant_productstudio_v1alpha import OutputImageConfig
from google.shopping.merchant_productstudio_v1alpha import UpscaleProductImageRequest
# Gets the merchant account ID from the user's configuration.
_ACCOUNT = configuration.Configuration().read_merchant_info()
# The name of the account to which the request is sent.
# Format: accounts/{account}
_NAME = f"accounts/{_ACCOUNT}"
def upscale_product_image(image_uri: str) -> None:
"""Upscales a product image.
Args:
image_uri: The URI of the input image.
"""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = ImageServiceClient(credentials=credentials)
# Creates the output config.
# Set `return_image_uri` to False to return the image bytes in the response.
output_config = OutputImageConfig(return_image_uri=True)
# Creates the input image.
# You can also use image bytes here instead of a URI.
input_image = InputImage(image_uri=image_uri)
# Creates the request.
request = UpscaleProductImageRequest(
name=_NAME, output_config=output_config, input_image=input_image
)
# Makes the request and catches and prints any error messages.
try:
print(f"Sending UpscaleProductImage request: {_NAME}")
response = client.upscale_product_image(request=request)
print("Upscaled product image response below:")
print(response)
except RuntimeError as e:
print("Request failed.")
print(e)
if __name__ == "__main__":
# The URI of the image to upscale.
# Replace with your image URI.
_IMAGE_URI = "https://services.google.com/fh/files/misc/ring_image_400_600.jpg"
upscale_product_image(_IMAGE_URI)
सामान्य गड़बड़ियां और समस्याएं
यहां कुछ सामान्य समस्याएं और उन्हें हल करने के तरीके दिए गए हैं.
"इनपुट इमेज ज़रूरी है"
अगर आपको गड़बड़ी का यह मैसेज मिलता है, तो अनुरोध के मुख्य हिस्से में input_image
जोड़ें:
Error message:
"error": {
"code": 400,
"message": "[input_image] Input image is required to generate product image background.",
"status": "INVALID_ARGUMENT",
...
}
"इमेज का कम से कम एक फ़ील्ड ज़रूरी है"
अगर आपको गड़बड़ी का यह मैसेज मिलता है, तो अनुरोध के मुख्य हिस्से में सिर्फ़ image_uri
या image_bytes
में से किसी एक को सही तरीके से भरें.
Error message:
"error": {
"code": 400,
"message": "[input_image.image] At least one field of image is required to generate product image background.",
"status": "INVALID_ARGUMENT",
...
}
उदाहरण के लिए, इसे पोस्ट करने पर गड़बड़ी दिखेगी.
POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground
{
"config": {
"product_description": "a jar",
"background_description": "sitting on a cracked stone surface surrounded by a cherry blossom tree and pink and white flowers in the background, high resolution, product photography, strong shadows and lights, creative"
}
}
"Config is required to generate product image background"
अगर आपको यह गड़बड़ी दिखती है, तो अनुरोध के मुख्य हिस्से में config
जोड़ें. साथ ही, product_description
और background_description
, दोनों फ़ील्ड में वैल्यू डालें:
Error message:
"error": {
"code": 400,
"message": "[config] Config is required to generate product image background.",
"status": "INVALID_ARGUMENT",
...
}
उदाहरण के लिए, इसे पोस्ट करने पर गड़बड़ी होती है.
POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground
{
"input_image": {
"image_uri": "https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
}
}
"प्रॉडक्ट की इमेज का बैकग्राउंड जनरेट करने के लिए, प्रॉडक्ट की जानकारी देना ज़रूरी है"
अगर आपको इनमें से कोई भी गड़बड़ी का मैसेज मिलता है, तो पक्का करें कि आपने config
में product_description
और background_description
, दोनों फ़ील्ड में वैल्यू डाली हों:
प्रॉडक्ट के ब्यौरे के मौजूद न होने का उदाहरण यहां दिया गया है:
Error message:
"error": {
"code": 400,
"message": "[config.product_description] Product description is required to generate product image background.",
"status": "INVALID_ARGUMENT",
...
}
बैकग्राउंड की जानकारी न होने का एक उदाहरण यहां दिया गया है:
Error message:
"error": {
"code": 400,
"message": "[config.background_description] Background description is required to generate product image background.",
"status": "INVALID_ARGUMENT",
...
}
उदाहरण के लिए, यहां दिया गया अनुरोध चलाने पर गड़बड़ी होगी.
POST https://merchantapi.googleapis.com/productstudio/v1alpha/accounts/{ACCOUNT_ID}/generatedImages:generateProductImageBackground
{
"input_image": {
"image_uri": "https://services.google.com/fh/files/misc/abundance_intention_bath_salts.jpg"
},
"config": {
}
}