Generative image super-resolution
This topic provides sample code for generative image super-resolution in common programming languages.
-
For real-time assistance, start an online consultation.
-
If you have questions about API access or usage for the Alibaba Cloud Vision AI Platform, join our DingTalk group (ID: 23109592) to contact us.
Overview
For a feature overview and the full list of request parameters, see generative image super-resolution.
SDK installation
For SDK dependencies for common programming languages, see SDK overview.
Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
An Alibaba Cloud account has full access to all API operations. We recommend that you use a RAM user for API calls or routine O&M. For more information, see Create a RAM user.
Do not save your AccessKey ID or AccessKey Secret in project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.
Configure environment variables on Linux and macOS
Open a terminal in IntelliJ IDEA.
Run the following commands to configure the environment variables.
Replace
<access_key_id>with the AccessKey ID of your RAM user and<access_key_secret>with the AccessKey Secret of your RAM user. If you need to configure more permissions, see Control access permissions using a RAM policy.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
Configure environment variables on Windows
Create a new environment variable file, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared. Then, restart the Windows operating system. The following example uses Windows 10.Open File Explorer, right-click This PC, and then select Properties.
In the left-side navigation pane, click Advanced system settings.
On the Advanced tab of the System Properties dialog box, click Environment Variables.
In the Environment Variables dialog box, click New.
In the New System Variable dialog box, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared.Restart the Windows operating system for the configurations to take effect.
Sample code
Image from an OSS bucket
For sample code in common languages to query asynchronous task results, see Query asynchronous task results.
// Import the dependency packages.
// The minimum SDK version required is imageenhan20190930 >= 1.0.12.
// You can reference the latest version of the SDK from this repository: https://mvnrepository.com/artifact/com.aliyun/imageenhan20190930
// <dependency>
// <groupId>com.aliyun</groupId>
// <artifactId>imageenhan20190930</artifactId>
// <version>${aliyun.imageenhan.version}</version>
// </dependency>
import com.aliyun.imageenhan20190930.models.GenerateSuperResolutionImageResponse;
import com.aliyun.sample.ExtendImageStyle.ExtendImageStyle1;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
public class GenerateSuperResolutionImage {
public static com.aliyun.imageenhan20190930.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object com.aliyun.teaopenapi.models.Config.
The object stores configuration information such as AccessKeyId, AccessKeySecret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The service endpoint.
config.endpoint = "imageenhan.cn-shanghai.aliyuncs.com";
return new com.aliyun.imageenhan20190930.Client(config);
}
public static void main(String[] args) throws Exception {
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.imageenhan20190930.Client client = GenerateSuperResolutionImage.createClient(accessKeyId, accessKeySecret);
com.aliyun.imageenhan20190930.models.GenerateSuperResolutionImageRequest generateSuperResolutionImageRequest = new com.aliyun.imageenhan20190930.models.GenerateSuperResolutionImageRequest()
.setImageUrl("https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png")
.setScale(2)
.setOutputFormat("jpg")
.setOutputQuality(100);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Print the API response.
GenerateSuperResolutionImageResponse response = client.generateSuperResolutionImageWithOptions(generateSuperResolutionImageRequest, runtime);
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
} catch (TeaException error) {
// Get the complete error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(error));
// Get a specific field from the response.
System.out.println(error.getCode());
}
}
}
# -*- coding: utf-8 -*-
# Import the dependency packages.
# The minimum SDK version required is imageenhan20190930 >= 2.0.10.
# You can reference the latest version of the SDK from this repository: https://pypi.org/project/alibabacloud-imageenhan20190930/
# pip install alibabacloud_imageenhan20190930
import os
from alibabacloud_imageenhan20190930.client import Client
from alibabacloud_imageenhan20190930.models import GenerateSuperResolutionImageRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
# Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The service endpoint.
endpoint='imageenhan.cn-shanghai.aliyuncs.com',
# The region ID that corresponds to the endpoint.
region_id='cn-shanghai'
)
generate_super_resolution_image_request = GenerateSuperResolutionImageRequest(
scale=2,
output_format='jpg',
output_quality=100,
image_url='https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png'
)
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.generate_super_resolution_image_with_options(generate_super_resolution_image_request, runtime)
# Get the complete response.
print(response.body)
except Exception as error:
# Get the complete error message.
print(error)
# Get a specific field from the response.
print(error.code)
<?php
// Install the dependency packages.
// The minimum SDK version required is imageenhan-20190930 >= 1.0.11.
// You can reference the latest version of the SDK from this repository: https://packagist.org/packages/alibabacloud/imageenhan-20190930
// composer require alibabacloud/imageenhan-20190930
use AlibabaCloud\SDK\Imageenhan\V20190930\Imageenhan;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Imageenhan\V20190930\Models\GenerateSuperResolutionImageRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class GenerateSuperResolutionImage {
/**
* Initialize the client with an AccessKey ID and an AccessKey Secret.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Imageenhan Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initialize the configuration object Darabonba\OpenApi\Models\Config.
// The object stores configuration information such as accessKeyId, accessKeySecret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The service endpoint.
$config->endpoint = "imageenhan.cn-shanghai.aliyuncs.com";
return new Imageenhan($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
$generateSuperResolutionImageRequest = new GenerateSuperResolutionImageRequest([
"imageUrl" => "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png",
"scale" => 2,
"outputFormat" => "jpg",
"outputQuality" => 100
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->generateSuperResolutionImageWithOptions($generateSuperResolutionImageRequest, $runtime);
# Get the complete response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Get the complete error message.
echo Utils::toJSONString($exception);
# Get a specific field from the response.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// $argv is a reserved array for input parameters and does not need to be modified.
GenerateSuperResolutionImage::main(array_slice($argv, 1));
// Install the dependency packages.
// npm install @alicloud/imageenhan20190930
// The minimum SDK version required is imageenhan20190930 >= 2.0.9.
// You can reference the latest version of the SDK from this repository: https://npmjs.com/package/@alicloud/imageenhan20190930
const ImageenhanClient = require('@alicloud/imageenhan20190930');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
let config = new OpenapiClient.Config({
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The service endpoint.
config.endpoint = `imageenhan.cn-shanghai.aliyuncs.com`;
const client = new ImageenhanClient.default(config);
let generateSuperResolutionImageRequest = new ImageenhanClient.GenerateSuperResolutionImageRequest({
imageUrl: "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png",
scale: 2,
outputQuality: 100,
outputFormat: "jpg",
});
let runtime = new TeaUtil.RuntimeOptions({});
client.generateSuperResolutionImageWithOptions(generateSuperResolutionImageRequest, runtime)
.then(function (generateSuperResolutionImageResponse) {
// Get the complete response.
console.log(generateSuperResolutionImageResponse);
// Get a specific field from the response.
console.log(generateSuperResolutionImageResponse.body.data);
}, function (error) {
// Get the complete error message.
console.log(error);
// Get a specific field from the response.
console.log(error.data.Code);
})
// Import the dependency packages.
// The minimum SDK version required is imageenhan20190930 >= 2.0.9.
// You can reference the latest version of the SDK from this repository: https://pkg.go.dev/github.com/alibabacloud-go/imageenhan-20190930/v2
// go get github.com/alibabacloud-go/imageenhan-20190930/v2
package main
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
imageenhan20190930 "github.com/alibabacloud-go/imageenhan-20190930/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
func main() {
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object &openapi.Config. The object stores configuration information such as AccessKeyId, AccessKeySecret, and Endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret)
}
// The service endpoint.
config.Endpoint = tea.String("imageenhan.cn-shanghai.aliyuncs.com")
client, err := imageenhan20190930.NewClient(config)
if err != nil {
fmt.Println(err.Error())
}
generateSuperResolutionImageRequest := &imageenhan20190930.GenerateSuperResolutionImageRequest{
ImageUrl: tea.String("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/imageenhan/ExtendImageStyle/ExtendImageStyle1.jpg"),
Scale: tea.Int32(2),
OutputFormat: tea.String("jpg"),
OutputQuality: tea.Int32(100),
}
runtime := &util.RuntimeOptions{}
generateSuperResolutionImageResponse, _err := client.GenerateSuperResolutionImageWithOptions(generateSuperResolutionImageRequest, runtime)
if _err != nil {
fmt.Println(_err.Error())
} else {
// Get the complete response.
fmt.Println(generateSuperResolutionImageResponse)
// Get a specific field from the response.
fmt.Println(generateSuperResolutionImageResponse.Body.Data)
}
}
// Install the dependency packages.
// dotnet add package AlibabaCloud.SDK.Imageenhan20190930
// The minimum SDK version required is imageenhan20190930 >= 1.0.9.
// You can reference the latest version of the SDK from this repository: https://nuget.org/packages/AlibabaCloud.SDK.Imageenhan20190930
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Imageenhan20190930.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initialize the client with an AccessKey ID and an AccessKey Secret.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Imageenhan20190930.Client CreateClient(string accessKeyId, string accessKeySecret)
{
// Initialize the configuration object AlibabaCloud.OpenApiClient.Models.Config. The object stores configuration information such as AccessKeyId, AccessKeySecret, and Endpoint.
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
// The service endpoint.
config.Endpoint = "imageenhan.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Imageenhan20190930.Client(config);
}
public static void Main(string[] args)
{
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
AlibabaCloud.SDK.Imageenhan20190930.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.SDK.Imageenhan20190930.Models.GenerateSuperResolutionImageRequest generateSuperResolutionImageRequest = new AlibabaCloud.SDK.Imageenhan20190930.Models.GenerateSuperResolutionImageRequest
{
ImageUrl = "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png",
Scale = 2,
OutputQuality = 100,
OutputFormat = "jpg",
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Imageenhan20190930.Models.GenerateSuperResolutionImageResponse generateSuperResolutionImageResponse = client.GenerateSuperResolutionImageWithOptions(generateSuperResolutionImageRequest, runtime);
// Get the complete response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(generateSuperResolutionImageResponse.Body));
// Get a specific field from the response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(generateSuperResolutionImageResponse.Body.Data));
}
catch (TeaException error)
{
// Print the error message if necessary.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Print the error message if necessary.
Console.WriteLine(error.Message);
}
}
}
}
Local file or public URL
For sample code in common languages to query asynchronous task results, see Query asynchronous task results.
// Import the dependency packages.
// The minimum SDK version required is imageenhan20190930 >= 1.0.12.
// You can reference the latest version of the SDK from this repository: https://mvnrepository.com/artifact/com.aliyun/imageenhan20190930
// <dependency>
// <groupId>com.aliyun</groupId>
// <artifactId>imageenhan20190930</artifactId>
// <version>${aliyun.imageenhan.version}</version>
// </dependency>
import com.aliyun.imageenhan20190930.models.GenerateSuperResolutionImageResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
public class GenerateSuperResolutionImage {
public static com.aliyun.imageenhan20190930.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object com.aliyun.teaopenapi.models.Config.
The object stores configuration information such as AccessKeyId, AccessKeySecret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The service endpoint.
config.endpoint = "imageenhan.cn-shanghai.aliyuncs.com";
return new com.aliyun.imageenhan20190930.Client(config);
}
public static void main(String[] args) throws Exception {
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.imageenhan20190930.Client client = GenerateSuperResolutionImage.createClient(accessKeyId, accessKeySecret);
// Scenario 1: Use a local file.
// InputStream inputStream = new FileInputStream(new File("tmp/GenerateSuperResolutionImage.png"));
// Scenario 2: Use a publicly accessible URL.
URL majorUrl = new URL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png");
InputStream inputStream = majorUrl.openConnection().getInputStream();
com.aliyun.imageenhan20190930.models.GenerateSuperResolutionImageAdvanceRequest generateSuperResolutionImageAdvanceRequest = new com.aliyun.imageenhan20190930.models.GenerateSuperResolutionImageAdvanceRequest()
.setImageUrlObject(inputStream)
.setScale(2)
.setOutputFormat("jpg")
.setOutputQuality(100);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Print the API response.
GenerateSuperResolutionImageResponse response = client.generateSuperResolutionImageAdvance(generateSuperResolutionImageAdvanceRequest, runtime);
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
} catch (TeaException error) {
// Get the complete error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(error));
// Get a specific field from the response.
System.out.println(error.getCode());
}
}
}
# -*- coding: utf-8 -*-
# Import the dependency packages.
# The minimum SDK version required is imageenhan20190930 >= 2.0.10.
# You can reference the latest version of the SDK from this repository: https://pypi.org/project/alibabacloud-imageenhan20190930/
# pip install alibabacloud_imageenhan20190930
import os
import io
from urllib.request import urlopen
from alibabacloud_imageenhan20190930.client import Client
from alibabacloud_imageenhan20190930.models import GenerateSuperResolutionImageAdvanceRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
# Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The service endpoint.
endpoint='imageenhan.cn-shanghai.aliyuncs.com',
# The region ID that corresponds to the endpoint.
region_id='cn-shanghai'
)
# Scenario 1: Use a local file.
#img = open(r'/tmp/GenerateSuperResolutionImage.png', 'rb')
# Scenario 2: Use a publicly accessible URL.
url = 'https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png'
img = io.BytesIO(urlopen(url).read())
generate_super_resolution_image_request = GenerateSuperResolutionImageAdvanceRequest(
scale=2,
output_format='jpg',
output_quality=100,
image_url_object=img
)
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.generate_super_resolution_image_advance(generate_super_resolution_image_request, runtime)
# Get the complete response.
print(response.body)
except Exception as error:
# Get the complete error message.
print(error)
# Get a specific field from the response.
print(error.code)
<?php
// Install the dependency packages.
// The minimum SDK version required is imageenhan-20190930 >= 1.0.11.
// You can reference the latest version of the SDK from this repository: https://packagist.org/packages/alibabacloud/imageenhan-20190930
// composer require alibabacloud/imageenhan-20190930
use AlibabaCloud\SDK\Imageenhan\V20190930\Imageenhan;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Imageenhan\V20190930\Models\GenerateSuperResolutionImageAdvanceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;
class GenerateSuperResolutionImageAdvance {
/**
* Initialize the client with an AccessKey ID and an AccessKey Secret.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Imageenhan Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initialize the configuration object Darabonba\OpenApi\Models\Config.
// The object stores configuration information such as accessKeyId, accessKeySecret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The service endpoint.
$config->endpoint = "imageenhan.cn-shanghai.aliyuncs.com";
return new Imageenhan($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
// Scenario 1: Use a local file.
//$file = fopen('/tmp/GenerateSuperResolutionImage.png', 'rb');
//$stream = new Stream($file);
// Scenario 2: Use a publicly accessible URL.
$file = fopen('https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png', 'rb');
$stream = new Stream($file);
$generateSuperResolutionImageRequest = new GenerateSuperResolutionImageAdvanceRequest([
"imageUrlObject" => $stream,
"scale" => 2,
"outputFormat" => "jpg",
"outputQuality" => 100
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->generateSuperResolutionImageAdvance($generateSuperResolutionImageRequest, $runtime);
# Get the complete response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Get the complete error message.
echo Utils::toJSONString($exception);
# Get a specific field from the response.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// $argv is a reserved array for input parameters and does not need to be modified.
GenerateSuperResolutionImageAdvance::main(array_slice($argv, 1));
// Install the dependency packages.
// npm install @alicloud/imageenhan20190930
// The minimum SDK version required is imageenhan20190930 >= 2.0.9.
// You can reference the latest version of the SDK from this repository: https://npmjs.com/package/@alicloud/imageenhan20190930
const ImageenhanClient = require('@alicloud/imageenhan20190930');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
const fs = require('fs');
const http = require('http');
const https = require('https');
let config = new OpenapiClient.Config({
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The service endpoint.
config.endpoint = `imageenhan.cn-shanghai.aliyuncs.com`;
const client = new ImageenhanClient.default(config);
const getResponse = function (httpClient, url) {
return new Promise((resolve, reject) => {
httpClient.get(url, function (response) {
resolve(response);
})
})
}
const request = async function () {
try {
let generateSuperResolutionImageAdvanceRequest = new ImageenhanClient.GenerateSuperResolutionImageAdvanceRequest();
// Scenario 1: Use a local file.
// const fileStream = fs.createReadStream('/tmp/GenerateSuperResolutionImage.png');
// generateSuperResolutionImageAdvanceRequest.imageUrlObject = fileStream;
// generateSuperResolutionImageAdvanceRequest.scale = 2;
// generateSuperResolutionImageAdvanceRequest.outputQuality = 100;
// generateSuperResolutionImageAdvanceRequest.outputFormat = "jpg";
// Scenario 2: Use a publicly accessible URL.
const url = new URL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png");
const httpClient = (url.protocol == "https:") ? https : http;
generateSuperResolutionImageAdvanceRequest.imageUrlObject = await getResponse(httpClient, url);
generateSuperResolutionImageAdvanceRequest.scale = 2;
generateSuperResolutionImageAdvanceRequest.outputQuality = 100;
generateSuperResolutionImageAdvanceRequest.outputFormat = "jpg";
let runtime = new TeaUtil.RuntimeOptions({ });
client.generateSuperResolutionImageAdvance(generateSuperResolutionImageAdvanceRequest, runtime)
.then(function(generateSuperResolutionImageResponse) {
// Get the complete response.
console.log(generateSuperResolutionImageResponse);
// Get a specific field from the response.
console.log(generateSuperResolutionImageResponse.body.data);
}, function(error) {
// Get the complete error message.
console.log(error);
// Get a specific field from the response.
console.log(error.data.Code);
})
} catch (error) {
console.log(error);
}
}();
// Import the dependency packages.
// The minimum SDK version required is imageenhan20190930 >= 2.0.9.
// You can reference the latest version of the SDK from this repository: https://pkg.go.dev/github.com/alibabacloud-go/imageenhan-20190930/v2
// go get github.com/alibabacloud-go/imageenhan-20190930/v2
package main
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
imageenhan20190930 "github.com/alibabacloud-go/imageenhan-20190930/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"net/http"
)
func main() {
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object &openapi.Config. The object stores configuration information such as AccessKeyId, AccessKeySecret, and Endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret)
}
// The service endpoint.
config.Endpoint = tea.String("imageenhan.cn-shanghai.aliyuncs.com")
client, err := imageenhan20190930.NewClient(config)
if err != nil {
fmt.Println(err.Error())
}
// Scenario 1: Use a local file.
//file, err := os.Open("/tmp/GenerateSuperResolutionImage.png")
//if err != nil {
// fmt.Println("can not open majorFile", err)
//}
//generateSuperResolutionImageRequest := &imageenhan20190930.GenerateSuperResolutionImageAdvanceRequest{
// ImageUrlObject: file,
// Scale: tea.Int32(2),
// OutputFormat: tea.String("jpg"),
// OutputQuality: tea.Int32(100),
//}
// Scenario 2: Use a publicly accessible URL.
httpClient := http.Client{}
file1, _ := httpClient.Get("https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png")
generateSuperResolutionImageRequest := &imageenhan20190930.GenerateSuperResolutionImageAdvanceRequest{
ImageUrlObject: file1.Body,
Scale: tea.Int32(2),
OutputFormat: tea.String("jpg"),
OutputQuality: tea.Int32(100),
}
runtime := &util.RuntimeOptions{}
generateSuperResolutionImageResponse, _err := client.GenerateSuperResolutionImageAdvance(generateSuperResolutionImageRequest, runtime)
if _err != nil {
fmt.Println(_err.Error())
} else {
// Get the complete response.
fmt.Println(generateSuperResolutionImageResponse)
// Get a specific field from the response.
fmt.Println(generateSuperResolutionImageResponse.Body.Data)
}
}
// Install the dependency packages.
// dotnet add package AlibabaCloud.SDK.Imageenhan20190930
// The minimum SDK version required is imageenhan20190930 >= 1.0.9.
// You can reference the latest version of the SDK from this repository: https://nuget.org/packages/AlibabaCloud.SDK.Imageenhan20190930
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Imageenhan20190930.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initialize the client with an AccessKey ID and an AccessKey Secret.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Imageenhan20190930.Client CreateClient(string accessKeyId, string accessKeySecret)
{
// Initialize the configuration object AlibabaCloud.OpenApiClient.Models.Config. The object stores configuration information such as AccessKeyId, AccessKeySecret, and Endpoint.
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
// The service endpoint.
config.Endpoint = "imageenhan.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Imageenhan20190930.Client(config);
}
public static void Main(string[] args)
{
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. To do so, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before running the sample code.
AlibabaCloud.SDK.Imageenhan20190930.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.SDK.Imageenhan20190930.Models.GenerateSuperResolutionImageAdvanceRequest generateSuperResolutionImageAdvanceRequest = new AlibabaCloud.SDK.Imageenhan20190930.Models.GenerateSuperResolutionImageAdvanceRequest
();
// Scenario 1: Use a local file.
//System.IO.StreamReader file = new System.IO.StreamReader(@"/tmp/GenerateSuperResolutionImage.png");
//generateSuperResolutionImageAdvanceRequest.ImageUrlObject = file.BaseStream;
//generateSuperResolutionImageAdvanceRequest.Scale = 2;
//generateSuperResolutionImageAdvanceRequest.OutputQuality = 100;
//generateSuperResolutionImageAdvanceRequest.OutputFormat = "jpg";
// Scenario 2: Use a publicly accessible URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/imageenhan/GenerateSuperResolutionImage.png");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
generateSuperResolutionImageAdvanceRequest.ImageUrlObject = stream;
generateSuperResolutionImageAdvanceRequest.Scale = 2;
generateSuperResolutionImageAdvanceRequest.OutputQuality = 100;
generateSuperResolutionImageAdvanceRequest.OutputFormat = "jpg";
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Imageenhan20190930.Models.GenerateSuperResolutionImageResponse generateSuperResolutionImageResponse = client.GenerateSuperResolutionImageAdvance(generateSuperResolutionImageAdvanceRequest, runtime);
// Get the complete response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(generateSuperResolutionImageResponse.Body));
// Get a specific field from the response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(generateSuperResolutionImageResponse.Body.Data));
}
catch (TeaException error)
{
// Print the error message if necessary.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Print the error message if necessary.
Console.WriteLine(error.Message);
}
}
}
}