すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:IMG

最終更新日:Nov 11, 2024

Image Processing (IMG) は、大量のデータを処理するのに役立つObject Storage service (OSS) が提供する、安全で費用対効果の高い信頼性の高い画像処理サービスです。 ソース画像をOSSにアップロードした後、RESTful APIを呼び出して、いつでもどこでもインターネットに接続されているデバイスで画像を処理できます。

サポートされるIMGパラメーターの詳細については、「概要」をご参照ください。

使用上の注意

  • このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。

  • このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。

IMGパラメータを使用した画像処理

  • 単一のIMGパラメータを使用して画像を処理し、処理した画像をローカルコンピュータに保存する

    # -*- coding: utf-8 -*-
    import os
    import oss2
    from oss2.credentials import EnvironmentVariableCredentialsProvider
    
    # Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
    # Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
    region = "cn-hangzhou"
    
    # Specify the name of the bucket. 
    bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
    
    # Specify the name of the image that you want to process. If the image is not stored in the root directory of the bucket, you must specify the path of the image. Example: example/example.jpg. 
    key = 'yourObjectName'
    # Specify the name of the processed image. 
    new_pic = 'LocalFileName'
    
    # If the image does not exist in the specified bucket, you must upload the image to the bucket. 
    # bucket.put_object_from_file(key, 'yourLocalFile')
    
    # Resize the image to 100 × 100 pixels and then save the resized image to your local computer. 
    style = 'image/resize,m_fixed,w_100,h_100'
    bucket.get_object_to_file(key, new_pic, process=style)
    
    # After the image is processed, you can delete the source image from the bucket if you no longer need the image. 
    # bucket.delete_object(key)
    # If you no longer need the processed image, you can delete the processed image. 
    # os.remove(new_pic)
  • 複数のIMGパラメータを使用して画像を処理し、処理した画像をローカルコンピュータに保存する

    次のサンプルコードは、複数のIMGパラメーターを使用してイメージを処理する方法の例を示しています。 IMGパラメータはスラッシュ (/) で区切られます。

    # -*- coding: utf-8 -*-
    import os
    import oss2
    from oss2.credentials import EnvironmentVariableCredentialsProvider
    
    # Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
    # Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
    region = "cn-hangzhou"
    
    # Specify the name of the bucket. 
    bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
    
    # Specify the name of the bucket in which the source image is stored. Example: examplebucket. 
    bucket_name = 'examplebucket'
    # Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the path of the image. Example: example/example.jpg. 
    key = 'exampledir/example.jpg'
    # Specify the name of the processed image. 
    new_pic = 'exampledir/newexample.jpg'
    
    # If the image that you want to process does not exist in the specified bucket, you must upload the image that is stored in the local path of the bucket. 
    # bucket.put_object_from_file(key, 'D:\\localpath\\example.jpg')
    
    # Resize the image to 100 × 100 pixels, rotate the image 90 degrees, and then save the processed image to your local computer. 
    style = 'image/resize,m_fixed,w_100,h_100/rotate,90'
    bucket.get_object_to_file(key, new_pic, process=style)
    # After the image is processed, you can delete the source image from the bucket if you no longer need the image. 
    # bucket.delete_object(key)
    # If you no longer need the processed image, you can delete the image. 
    # os.remove(new_pic)

画像スタイルを使用して画像を処理する

イメージスタイル内に複数のIMGパラメータをカプセル化し、イメージスタイルを使用してイメージを処理できます。 詳細については、「イメージスタイル」をご参照ください。 次のコードは、イメージスタイルを使用してイメージを処理する方法の例を示しています。

# -*- coding: utf-8 -*-
import os
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the name of the bucket in which the source image is stored. Example: examplebucket. 
bucket_name = 'examplebucket'
# Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the path of the image. Example: example/example.jpg. 
key = 'exampledir/example.jpg'
# Specify the name of the processed image. 
new_pic = 'exampledir/newexample.jpg'

# If the image that you want to process does not exist in the specified bucket, you must upload the image that is stored in the local path of the bucket. 
# bucket.put_object_from_file(key, 'D:\\localpath\\example.jpg')

# Use a custom image style to process the image. Set yourCustomStyleName to the name of the image style that you created in the OSS console. 
style = 'style/yourCustomStyleName'
# Save the processed image to your local computer. 
bucket.get_object_to_file(key, new_pic, process=style)
# After the image is processed, you can delete the source image from the bucket if you no longer need the image. 
# bucket.delete_object(key)
# If you no longer need the processed image, you can delete the image. 
# os.remove(new_pic)

処理された画像を保存する

ImgSaveAs操作を呼び出して、処理した画像を特定のバケットに保存できます。 次のサンプルコードは、処理されたイメージを保存する方法の例を示します。

# -*- coding: utf-8 -*-
import os
import base64
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the name of the bucket in which the source image is stored. 
source_bucket_name = 'srcbucket'
# Specify the name of the bucket in which you want to save the processed image. The bucket must be within the same region as the bucket in which the source image is stored. 
target_bucket_name = 'destbucket'
# Specify the name of the source image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: example/example.jpg. 
source_image_name = 'example/example.jpg'

# Resize the image to 100 × 100 pixels. 
style = 'image/resize,m_fixed,w_100,h_100'
# Specify the name of the processed image. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
target_image_name = 'exampledir/example.jpg'
process = "{0}|sys/saveas,o_{1},b_{2}".format(style, 
    oss2.compat.to_string(base64.urlsafe_b64encode(oss2.compat.to_bytes(target_image_name))),
    oss2.compat.to_string(base64.urlsafe_b64encode(oss2.compat.to_bytes(target_bucket_name))))
result = bucket.process_object(source_image_name, process)
print(result)

IMGパラメータを含む署名付きオブジェクトURLを生成する

プライベートオブジェクトのURLに署名する必要があります。 署名付きURLの末尾にIMGパラメーターを追加することはできません。 プライベートイメージオブジェクトを処理する場合は、署名にIMGパラメータを追加します。 次のサンプルコードは、署名にIMGパラメーターを追加する方法の例を示しています。

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the name of the bucket. Example: examplebucket. 
bucket_name = 'examplebucket'
# Specify the name of the bucket in which the image is stored. If the image is not stored in the root directory of the bucket, you must specify the full path of the image. Example: exampledir/example.jpg. 
key = 'exampledir/example.jpg'

# If the image is not stored in the specified bucket, you must upload the image to the bucket. 
# bucket.put_object_from_file(key, 'D:\\localpath\\example.jpg')
# Resize the image to 100 × 100 pixels and rotate the image 90 degrees. 
style = 'image/resize,m_fixed,w_100,h_100/rotate,90'
# Generate a signed object URL that includes IMG parameters. Set the validity period of the URL to 600. Unit: seconds. 
url = bucket.sign_url('GET', key, 10 * 60, params={'x-oss-process': style})
print(url)

関連ドキュメント

IMGの使用方法に関する完全なサンプルコードについては、『GitHub』をご参照ください。