edit-icon download-icon

Image processing

Last Updated: Dec 21, 2017

OSS provides Image processing service that has features like massive processing power, security, low costs, and high reliability.

By uploading and storing source images in OSS, you can process images anytime, anywhere, and on any Internet device through a simple RESTful interface.

The image processing offers image processing interfaces. For image uploads, use OSS upload interfaces. You can build your own image processing service based on OSS image processing.

Image processing basic features

OSS image processing provides the following features:

Usage of image processing

Image processing uses the standard HTTP GET request for access. All processing parameters are encoded in the QueryString in the URL.

Anonymous access

If the access permission of the image object is public read, as shown in the following table, you can access image processing anonymously.

Bucket permission Object permission
Public-read or
Public-read-write
Default
Any permission Public-read or
Public-read-write

Access image processing anonymously using the third-level domain in the following format:

  1. http://bucket.<endpoint>/object?x-oss-process=image/action,parame_value
  • bucket: Name of the user’s bucket
  • endpoint: The access domain name for the data center of the user’s bucket
  • object: The image object uploaded by the user to OSS
  • image: The identifier reserved by the image processing
  • action: Operations by the user on images, such as scaling, cropping, and rotating
  • parameter: Parameters corresponding to the operations by the user on the image

For example,

  1. http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100

Custom style: Access image processing anonymously using the third-level domain in the following format:

  1. http://bucket.<endpoint>/object?x-oss-process=x-oss-process=style/name
  • style: The identifier reserved by the stem for the user’s custom style
  • name: Name of the custom style, that is, the rule name of the console-defined style

For example,

  1. http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=style/oss-pic-style-w-100

Cascading process can help you implement multiple operations on an image. The format is as follows:

  1. http://bucket.<endpoint>/object?x-oss-process=image/action,parame_value/action,parame_value/...

For example,

  1. http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100/rotate,90

Image processing supports HTTPS access. For example,

  1. https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_100

Authorized access

For objects with private permissions, as shown in the following table, you must be authorized to access image processing.

Bucket permission Object permission
Private Default
Any permission Private

The image processing URL code generated with signature is as follows:

  1. # -*- coding: utf-8 -*-
  2. import oss2
  3. endpoint = '<endpoint, for example, http://oss-cn-hangzhou.aliyuncs.com>'
  4. access_key_id = '<access_key_id>'
  5. access_key_secret = '<access_key_secret>'
  6. bucket_name = '<bucket_name>'
  7. key = 'example.jpg'
  8. # Create a bucket object. All object-related interfaces can be implemented through the bucket object
  9. bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
  10. # Upload the example image
  11. bucket.put_object_from_file(key, 'example.jpg')
  12. # Generate a signed URL. The expiration time is 10 minutes
  13. style = 'image/resize,m_fixed,w_100,h_100/rotate,90'
  14. url = bucket.sign_url('GET', key, 10 * 60, params={'x-oss-process': style})
  15. print(url)

Note:

  • Authorized access supports custom styles, HTTPS, and cascading processing.
  • sign_url unit of the time-out value is seconds.

SDK access

You can directly use SDK to access and process image objects with any permissions.

Note:

  • Complete image processing code can be found at: GitHub
  • Image object processing by SDK supports custom styles, HTTPS, and cascading processing.

Basic operations

Basic image processing operations include getting image information, format conversion, scaling, cropping, rotating, effects and watermarks.

  1. # -*- coding: utf-8 -*-
  2. import os
  3. import oss2
  4. endpoint = '<endpointFor example, 'http://oss-cn-hangzhou.aliyuncs.com>'
  5. access_key_id = '<access_key_id>'
  6. access_key_secret = '<access_key_secret>'
  7. bucket_name = '<bucket_name>'
  8. key = 'example.jpg'
  9. new_pic = 'example-new.jpg'
  10. # Create a bucket object. All object-related interfaces can be implemented through the bucket object
  11. bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
  12. # Uploading the example image
  13. bucket.put_object_from_file(key, 'example.jpg')
  14. # Scaling
  15. style = 'image/resize,m_fixed,w_100,h_100'
  16. bucket.get_object_to_file(key, new_pic, process=style)
  17. # Cropping
  18. style = 'image/crop,w_100,h_100,x_100,y_100,r_1'
  19. bucket.get_object_to_file(key, new_pic, process=style)
  20. # Rotating
  21. style = 'image/rotate,90'
  22. bucket.get_object_to_file(key, new_pic, process=style)
  23. # Sharpening
  24. style = 'image/sharpen,100'
  25. bucket.get_object_to_file(key, new_pic, process=style)
  26. # adding a text watermark
  27. style = 'image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ'
  28. bucket.get_object_to_file(key, new_pic, process=style)
  29. # Converting image formats
  30. style = 'image/format,png'
  31. bucket.get_object_to_file(key, new_pic, process=style)
  32. # Deleting the example image
  33. bucket.delete_object(key)
  34. # Clearing the local file
  35. os.remove(new_pic)

Note: The get_object method also supports image processing.

Custom styles

  1. # -*- coding: utf-8 -*-
  2. import os
  3. import oss2
  4. endpoint = '<endpoint, For example, 'http://oss-cn-hangzhou.aliyuncs.com>'
  5. access_key_id = '<access_key_id>'
  6. access_key_secret = '<access_key_secret>'
  7. bucket_name = '<bucket_name>'
  8. key = 'example.jpg'
  9. new_pic = 'example-new.jpg'
  10. # Create a bucket object. All object-related interfaces can be implemented through the bucket object
  11. bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
  12. # Uploading the example image
  13. bucket.put_object_from_file(key, 'example.jpg')
  14. # Custom styles
  15. style = 'style/oss-pic-style-w-100'
  16. # Image processing
  17. bucket.get_object_to_file(key, new_pic, process=style)
  18. # Deleting the example image
  19. bucket.delete_object(key)
  20. # Clearing the local file
  21. os.remove(new_pic)

Note: The get_object also supports the image processing feature.

Cascading processing

  1. # -*- coding: utf-8 -*-
  2. import os
  3. import oss2
  4. endpoint = '<endpoint, for example, http://oss-cn-hangzhou.aliyuncs.com>'
  5. access_key_id = '<access_key_id>'
  6. access_key_secret = '<access_key_secret>'
  7. bucket_name = '<bucket_name>'
  8. key = 'example.jpg'
  9. new_pic = 'example-new.jpg'
  10. # Create a bucket object. All object-related interfaces can be implemented through the bucket object
  11. bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
  12. # Uploading the example image
  13. bucket.put_object_from_file(key, 'example.jpg')
  14. # Cascading processing
  15. style = 'image/resize,m_fixed,w_100,h_100/rotate,90'
  16. # Image processing
  17. bucket.get_object_to_file(key, new_pic, process=style)
  18. # Deleting the example image
  19. bucket.delete_object(key)
  20. # Clearing the local file
  21. os.remove(new_pic)

Note: The get_object also supports the image processing feature.

Thank you! We've received your feedback.