Image API بخشی از API فرعی Product Studio است. این ابزار از ابزارهای هوش مصنوعی مولد Google Product Studio (GPS) API برای تولید و بهینه سازی تصاویر محصول استفاده می کند. می توانید از آن برای بهبود تعامل و تبدیل مشتری استفاده کنید.
با استفاده از API چه چیزی می توانید ایجاد کنید؟
- تصاویر محصول با پسزمینه تولید شده توسط هوش مصنوعی بر اساس اطلاعات محصول ارائه شده و یک پیام متنی
- تصاویر محصول با پس زمینه تصویر اصلی حذف شده است
- تصاویر محصول که در آن وضوح تصویر اصلی افزایش یافته است
یک پس زمینه تصویر ایجاد کنید
روش GenerateProductImageBackground
میتواند تصاویر محصول با پسزمینههای ایجاد شده توسط هوش مصنوعی را با استفاده از تصویر اصلی محصول شما و یک پیام متنی که جزئیات پسزمینه را نشان میدهد، ایجاد کند.
API می پذیرد:
- تصویر محصول : می تواند یکی از موارد زیر باشد:
- یک URI که به تصویر محصول اشاره میکند (مانند
{"image_uri": "https://my-store.com/img/1.png"}
) - بایتهای تصویر خام، مانند
{"image_bytes": b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01'}
- یک URI که به تصویر محصول اشاره میکند (مانند
- Prompt : رشته ای که زمینه تولید را مشخص می کند:
-
product_description
: توضیحات محصول. -
background_description
: شرح پس زمینه مورد نظر.
-
حذف پس زمینه تصویر
روش RemoveProductImageBackground
می تواند تصاویری با پس زمینه حذف شده از تصویر محصول اصلی شما ایجاد کند.
API می پذیرد:
- تصویر محصول : می تواند یکی از موارد زیر باشد:
- یک URI که به تصویر محصول اشاره میکند، مانند
{"image_uri": "https://my-store.com/img/1.png"}
- بایتهای تصویر خام، مانند
{"image_bytes": b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01'}
- یک URI که به تصویر محصول اشاره میکند، مانند
- رنگ پسزمینه : رنگ پسزمینه پس از حذف، با فرمت RGB داده میشود. اگر رنگی مشخص نشده باشد، پس زمینه یک تصویر شفاف RGBA 4 کانالی خواهد بود.
یک تصویر را ارتقا دهید
روش UpscaleProductImage
می تواند تصاویری تولید کند که وضوح تصویر محصول اصلی شما افزایش می یابد.
API تصویر محصول را می پذیرد. می تواند یکی از موارد زیر باشد:
- یک URI که به تصویر محصول اشاره میکند، مانند
{"image_uri": "https://my-store.com/img/1.png"}
- بایت های تصویر خام، مانند،
{"image_bytes": "image_bytes"}
از کتابخانه های مشتری استفاده کنید
توصیه می کنیم برای ارسال درخواست های خود از کتابخانه های مشتری استفاده کنید. اگر کتابخانه سرویس گیرنده پشتیبانی شده برای زبان شما در دسترس نیست، میتوانیم فایلهای کتابخانه را مستقیماً برای شما (در ایمیل یا پوشه درایو مشترک) برای نصب، به عنوان مثال، در پروژه Maven خود ارائه کنیم.
نمونه ها
مثالهای زیر از API برای تولید یا بهینهسازی تصاویر محصول استفاده میکنند.
یک پس زمینه برای تصویر محصول ایجاد کنید
مثال نشان می دهد که چگونه می توان یک تصویر محصول را با پس زمینه انتخابی خود ایجاد کرد.
درخواست کنید
بدنه درخواست شامل تصویر (URI یا بایت) و درخواست تولید پسزمینه است. در اینجا نمونه ای از ساختار درخواست با استفاده از URI تصویر آمده است:
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"
}
}
نمونه کد
در اینجا نمونه کدی وجود دارد که نحوه تولید پسزمینه تصویر محصول را نشان میدهد:
جاوا
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);
}
}
پایتون
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)
پس زمینه را از تصویر محصول حذف کنید
این مثال نحوه حذف پسزمینه تصویر محصول را نشان میدهد.
درخواست کنید
بدنه درخواست شامل تصویر (URI یا بایت) و به صورت اختیاری، رنگی است که باید پسزمینه را جایگزین کنید.
یادداشت ها:
- مقادیر رنگ را در قالب RGB در محدوده [0-255] مشخص کنید.
- اگر رنگ پس زمینه را مشخص نکنید، یک تصویر شفاف RGBA 4 کاناله خواهد بود.
در اینجا نمونه ای از ساختار درخواست با استفاده از URI تصویر و تنظیم پس زمینه سبز (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"
}
}
نمونه کد
در اینجا نمونه کدی وجود دارد که نحوه حذف پسزمینه تصویر محصول را نشان میدهد:
جاوا
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);
}
}
پایتون
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)
تصویر محصول را ارتقا دهید
مثال نشان می دهد که چگونه کیفیت و اندازه تصویر محصول خود را افزایش دهید.
درخواست کنید
بدنه درخواست حاوی تصویر (URI یا بایت) است. در اینجا نمونه ای از ساختار درخواست با استفاده از 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"
}
}
نمونه کد
در اینجا نمونه کدی وجود دارد که نحوه ارتقاء تصویر محصول را نشان می دهد:
جاوا
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);
}
}
پایتون
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
در بدنه درخواست اضافه کنید و 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"
}
}
"توضیح محصول برای ایجاد پس زمینه تصویر محصول مورد نیاز است"
اگر یکی از دو پیام خطای زیر را دریافت کردید، مطمئن شوید که هر دو product_description
و background_description
در config
پر کنید:
این نمونه ای از توضیحات محصول گم شده است:
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": {
}
}