Image Processing (IMG) provided by Object Storage Service (OSS) is a secure, cost-effective, and highly reliable image processing service that can process large amounts of data. After you upload source images to OSS, you can call RESTful APIs to process the images anytime, anywhere, and on all Internet devices.

For more information about IMG parameters, see Parameters.

For more information about the complete sample code, visit GitHub.

Use a single IMG parameter to process images

  • The following code provides an example on how to use a single IMG parameter to process an image and save the image to your computer:
    # -*- coding: utf-8 -*-
    import os
    import oss2
    
    # In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
    endpoint = 'https://oss-cn-hangzhou.aliyuncs.com'
    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    access_key_id = '<yourAccessKeyId>'
    access_key_secret = '<yourAccessKeySecret>'
    # The name of the bucket in which the source image is stored. 
    bucket_name = '<yourBucketName>'
    # The name of the source image. If the image is not stored in the root directory of the bucket, you must add the access path of the image. Example: example/example.jpg. 
    key = '<yourObjectName>'
    # Specify the name of the processed image. 
    new_pic = '<LocalFileName>'
    
    # Specify the bucket instance. You must use the bucket instance to call all object-related methods. 
    bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
    
    # 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>')
    
    # After you resize the image to a height and width of 100 pixels, save the image to your computer. 
    style = 'image/resize,m_fixed,w_100,h_100'
    bucket.get_object_to_file(key, new_pic, process=style)
    
    # After the IMG is complete, you can delete the source image from the bucket if you no longer use the image. 
    # bucket.delete_object(key)
    # If you no longer use the processed image, you can delete the processed image. 
    # os.remove(new_pic)                      
  • Use multiple IMG parameters to process an image and save the image to your computer
    The following code provides an example on how to use multiple IMG parameters to process an image. Separate these parameters with forward slashes (/).
    # -*- coding: utf-8 -*-
    import os
    import oss2
    
    # 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'
    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    access_key_id = 'yourAccessKeyId'
    access_key_secret = 'yourAccessKeySecret'
    # 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 add the full path of the image. Example: exampledir/example.jpg. 
    key = 'exampledir/example.jpg'
    # Specify the name of the processed image. 
    new_pic = 'exampledir/newexample.jpg'
    
    # Specify the bucket instance. You must use the bucket instance to call all object-related methods. 
    bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
    
    # If the image is not stored in the specified bucket, you must upload the image from the local path to the specified bucket. 
    # bucket.put_object_from_file(key, 'D:\\localpath\\example.jpg')
    
    # After you resize the image to a height and width of 100 pixels, rotate the image 90 degrees, and save the image to your computer. 
    style = 'image/resize,m_fixed,w_100,h_100/rotate,90'
    bucket.get_object_to_file(key, new_pic, process=style)
    # After the IMG is complete, you can delete the source image from the bucket if you no longer use the image. 
    # bucket.delete_object(key)
    # If you no longer use the processed image, you can delete it. 
    # os.remove(new_pic)                           

Use an image style to process images

You can encapsulate multiple IMG parameters in a style, and then use the style to concurrently process multiple images. For more information, see Image styles. The following code provides an example on how to use an image style to process an image:
# -*- coding: utf-8 -*-
import os
import oss2
# 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'
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
access_key_id = 'yourAccessKeyId'
access_key_secret = 'yourAccessKeySecret'
# 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 add the full path of the image. Example: exampledir/example.jpg. 
key = 'exampledir/example.jpg'
# Specify the name of the processed image. 
new_pic = 'exampledir/newexample.jpg'

# Specify the bucket instance. You must use the bucket instance to call all object-related methods. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
# If the image is not stored in the specified bucket, you must upload the image from the local path to the specified bucket. 
# bucket.put_object_from_file(key, 'D:\\localpath\\example.jpg')

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

Save processed images

You can call the ImgSaveAs operation to save the processed images to a specified bucket. The following code provides an example on how to save a processed image:
# -*- coding: utf-8 -*-
import os
import base64
import oss2
# 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'
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
access_key_id = 'yourAccessKeyId'
access_key_secret = 'yourAccessKeySecret'
# Specify the name of the bucket in which the source image is stored. 
source_bucket_name = 'srcbucket'
# Specify the name of the bucket to which the processed image is saved. The bucket must be within the same region as the bucket that stores the source image. 
taget_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 add the full path of the image. Example: example/example.jpg. 
source_image_name = 'example/example.jpg'

# Specify the bucket instance. You must use the bucket instance to call all object-related methods. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, source_bucket_name)

# Resize the image to a height and width of 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 add 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(taget_bucket_name))))
result = bucket.process_object(source_image_name, process)
print(result)

Generate a signed object URL that includes IMG parameters

URLs of private objects must be signed. IMG parameters cannot be added to the end of a signed URL. If you want to process a private object, add IMG parameters to the signature. The following code provides an example on how to add IMG parameters to a signature:
# -*- coding: utf-8 -*-
import oss2

# 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'
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to access OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
access_key_id = 'yourAccessKeyId'
access_key_secret = 'yourAccessKeySecret'
# Specify the bucket name. Example: examplebucket. 
bucket_name = 'examplebucket'
# Specify the name of the bucket in which the source image is stored. If the image is not stored in the root directory of the bucket, you must add the full path of the image. Example: exampledir/example.jpg. 
key = 'exampledir/example.jpg'

# Specify the bucket instance. You must use the bucket instance to call all object-related methods. 
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
# If the image does not exist in the specified bucket, you must upload the image to the bucket. 
# bucket.put_object_from_file(key, 'D:\\localpath\\example.jpg')
# After you resize the image to a height and width of 100 pixels, 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 to 10 minutes. Unit: seconds. 
url = bucket.sign_url('GET', key, 10 * 60, params={'x-oss-process': style})
print(url)