All Products
Search
Document Center

Object Storage Service:Image processing (Python SDK V1)

Last Updated:Nov 28, 2025

Image processing is a scalable, secure, cost-effective, and highly reliable service provided by Object Storage Service (OSS). After you upload an image to OSS, you can use simple RESTful APIs to process the image on any internet-connected device.

For more information about supported image processing parameters, see Image processing.

Notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

Process images using image processing parameters

  • Process an image using a single image processing parameter and save the result as a local file

    # -*- 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 set.
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Specify the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
    # Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
    region = "cn-hangzhou"
    
    # Set examplebucket to the name of your bucket.
    bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
    
    # Set yourObjectName to the name of the destination image. If the image is not in the root directory of the bucket, you must specify the full path, such as example/example.jpg.
    key = 'yourObjectName'
    # Set LocalFileName to the name of the processed image.
    new_pic = 'LocalFileName'
    
    # If the destination image is not in the specified bucket, upload the image to the bucket.
    # bucket.put_object_from_file(key, 'yourLocalFile')
    
    # Resize the image to a fixed width and height of 100 px and save it to your local machine.
    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 it is no longer needed.
    # bucket.delete_object(key)
    # If the processed image is no longer needed, you can delete it.
    # os.remove(new_pic)
  • Process an image using multiple image processing parameters and save the result as a local file

    When you use multiple image processing parameters, separate them with forward slashes (/).

    # -*- 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 set.
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    
    # Specify the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
    endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
    # Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
    region = "cn-hangzhou"
    
    # Set examplebucket to the name of your bucket.
    bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)
    
    # Specify the name of the bucket where the source image is stored, such as examplebucket.
    bucket_name = 'examplebucket'
    # Specify the name of the source image. If the image is not in the root directory of the bucket, you must specify the full path of the image, such as exampledir/example.jpg.
    key = 'exampledir/example.jpg'
    # Specify the name of the processed image.
    new_pic = 'exampledir/newexample.jpg'
    
    # If the image is not in the specified bucket, upload the image from your local path to the bucket.
    # bucket.put_object_from_file(key, 'D:\\localpath\\example.jpg')
    
    # Resize the image to a fixed width and height of 100 px, rotate it by 90 degrees, and then save it to your local machine.
    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 it is no longer needed.
    # bucket.delete_object(key)
    # If the processed image is no longer needed, you can delete it.
    # os.remove(new_pic)

Process images using image styles

You can encapsulate multiple image processing parameters into a style and then use the style to process images in batches. For more information, see Image styles. The following code shows how to process an image using an image style:

# -*- 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 set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

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

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

# If the image is not in the specified bucket, upload the image from your local path to the bucket.
# bucket.put_object_from_file(key, 'D:\\localpath\\example.jpg')

# Process the image using a custom style. 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 machine.
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 it is no longer needed.
# bucket.delete_object(key)
# If the processed image is no longer needed, you can delete it.
# os.remove(new_pic)

Image processing persistence

You can use the ImgSaveAs API to save a processed image to a specified bucket. The following code shows how to perform an image processing persistence operation:

# -*- 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 set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

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

# Specify the name of the bucket where the source image is stored.
source_bucket_name = 'srcbucket'
# Specify the name of the bucket where you want to store the processed image. This bucket must be in the same region as the source bucket.
target_bucket_name = 'destbucket'
# Specify the name of the source image. If the image is not in the root directory of the bucket, you must specify the full path of the file, such as example/example.jpg.
source_image_name = 'example/example.jpg'

# Resize the image to a fixed width and height of 100 px.
style = 'image/resize,m_fixed,w_100,h_100'
# Specify the name of the processed image. If the image is not in the root directory of the bucket, you must specify the full path of the file, such as 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)

Generate a signed URL for a file with image processing parameters

Access URLs for private files are signed. OSS does not allow you to directly add image processing parameters to a signed URL. If you want to process a private file, you must add the image processing parameters to the signature. The following code provides an example:

# -*- 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 set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

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

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

# If the image is not in the specified bucket, upload the image to the destination bucket.
# bucket.put_object_from_file(key, 'D:\\localpath\\example.jpg')
# Resize the image to a fixed width and height of 100 px and then rotate it by 90 degrees.
style = 'image/resize,m_fixed,w_100,h_100/rotate,90'
# Generate a signed URL that is valid for 10 minutes. The validity period is measured in seconds.
url = bucket.sign_url('GET', key, 10 * 60, params={'x-oss-process': style})
print(url)

References

For the complete sample code for image processing, see the GitHub example.