Image pixelation

更新时间:
复制 MD 格式

You can use mosaics, Gaussian blurs, or solid color shapes to blur a specific area of an image for privacy protection. This topic describes how to use the image blurring feature of Intelligent Media Management (IMM).

Prerequisites

  • An AccessKey pair is created and obtained. For more information, see Create an AccessKey pair.

  • OSS is activated, a bucket is created, and objects are uploaded to the bucket. For more information, see Upload objects.

  • IMM is activated. For more information, see Activate IMM.

  • A project is created in the IMM console. For more information about how to create a project by using the IMM console, see Create a project.

    Note
    • You can also call the CreateProject operation to create a project. For more information, see CreateProject.

    • You can call the ListProjects operation to query existing projects in a specific region. For more information, see ListProjects.

Usage

Call the AddImageMosaic operation to blur an image.

Image processing information

  • IMM project: test-project

  • The path of the image to be blurred: oss://test-bucket/test-object.jpg

  • Blurring method: mosaic squares with a width and height of 100 pixels

  • Output image format: JPG

  • The path of the output image: oss://test-bucket/test-target-object.jpg

  • Source image:

    test-object

Sample request

{
    "ProjectName": "test-project",
    "SourceURI": "oss://test-bucket/test-object.jpg",
    "TargetURI": "oss://test-bucket/test-target-object.jpg",
    "ImageFormat": "jpg",
    "Quality": "81",
    "Targets": "[{\"Type\": \"square\", \"Boundary\": {\"ReferPos\": \"topleft\", \"X\": 20, \"Y\": 20, \"Height\": 100, \"Width\": 100}, \"MosaicRadius\": 10, \"BlurRadius\": 5, \"Sigma\": 1, \"Color\": \"black\"}]"
}

Sample success response

{
  "RequestId" : "5F2389FE-A5E3-076F-B071-43A33*****",
}

Output imagetest-object

Sample code

The following sample code blurs an image by using IMM SDK for Python:

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys
import os
from typing import List

from alibabacloud_imm20200930.client import Client as imm20200930Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_imm20200930 import models as imm_20200930_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
        access_key_id: str,
        access_key_secret: str,
    ) -> imm20200930Client:
        """
        Use your AccessKey ID and AccessKey secret to initialize the client.
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            access_key_id=access_key_id,
            access_key_secret=access_key_secret
        )
        # Specify the IMM endpoint.
        config.endpoint = f'imm.cn-beijing.aliyuncs.com'
        return imm20200930Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M.
        # We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code for data security reasons.
        # In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For information about how to configure environment variables, visit https://help.aliyun.com/document_detail/2361894.html.
        imm_access_key_id = os.getenv("AccessKeyId")
        imm_access_key_secret = os.getenv("AccessKeySecret")
        client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
        targets_0boundary = imm_20200930_models.AddImageMosaicRequestTargetsBoundary(
            refer_pos='topleft',
            x=20,
            y=20,
            height=100,
            width=100
        )
        targets_0 = imm_20200930_models.AddImageMosaicRequestTargets(
            boundary=targets_0boundary,
            type='square',
            mosaic_radius=10,
            blur_radius=5,
            sigma=1,
            color='black'
        )
        add_image_mosaic_request = imm_20200930_models.AddImageMosaicRequest(
            project_name='test-project',
            source_uri='oss://test-bucket/test-object.jpg',
            target_uri='oss://test-bucket/test-target-object.jpg',
            image_format='jpg',
            quality=81,
            targets=[
                targets_0
            ]
        )
        runtime = util_models.RuntimeOptions()
        try:
            # Write your code to print the response of the API operation if necessary.
            client.add_image_mosaic_with_options(add_image_mosaic_request, runtime)
        except Exception as error:
            # Print the error message if necessary.
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M.
        # We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code for data security reasons.
        # In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For information about how to configure environment variables, visit https://help.aliyun.com/document_detail/2361894.html.
        imm_access_key_id = os.getenv("AccessKeyId")
        imm_access_key_secret = os.getenv("AccessKeySecret")
        client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
        targets_0boundary = imm_20200930_models.AddImageMosaicRequestTargetsBoundary(
            refer_pos='topleft',
            x=20,
            y=20,
            height=100,
            width=100
        )
        targets_0 = imm_20200930_models.AddImageMosaicRequestTargets(
            boundary=targets_0boundary,
            type='square',
            mosaic_radius=10,
            blur_radius=5,
            sigma=1,
            color='black'
        )
        add_image_mosaic_request = imm_20200930_models.AddImageMosaicRequest(
            project_name='test-project',
            source_uri='oss://test-bucket/test-object.jpg',
            target_uri='oss://test-bucket/test-target-object.jpg',
            image_format='jpg',
            quality=81,
            targets=[
                targets_0
            ]
        )
        runtime = util_models.RuntimeOptions()
        try:
            # Write your code to print the response of the API operation if necessary.
            await client.add_image_mosaic_with_options_async(add_image_mosaic_request, runtime)
        except Exception as error:
            # Print the error message if necessary.
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])

Billing

During image pixelation, the following billable items are generated on both OSS and IMM sides:

  • OSS side: For detailed pricing information, see OSS Pricing.

    API

    Billable item

    Description

    GetObject

    GET requests

    You are charged request fees based on the number of successful requests.

    Retrieval of IA objects

    If IA objects are retrieved, you are charged IA data retrieval fees based on the size of retrieved IA objects.

    Archive Data Direct Read Retrieval Capacity

    If Archive objects in a bucket for which real-time access is enabled are retrieved, you are charged Archive data retrieval fees based on the size of retrieved Archive objects.

    Transfer acceleration

    If you enable transfer acceleration and use an acceleration endpoint to access your bucket, you are charged transfer acceleration fees based on the data size.

    PutObject

    PUT requests

    You are charged request fees based on the number of successful requests.

    Storage fees

    You are charged storage fees based on the storage class, size, and storage duration of the object.

    HeadObject

    GET requests

    You are charged request fees based on the number of successful requests.

  • IMM side: For detailed pricing information, see IMM billable items.

    Important

    Starting from 11:00 UTC+8 on July 28, 2025, the IMM image pixelation service will be upgraded from a free model to a paid model. For more information, see IMM billing adjustment announcement.

    API

    Billable item

    Description

    AddImageMosaic

    ImageMosaic

    You are charged image pixelation fees based on the number of successful requests.