图片模糊处理

图片模糊处理可以对一张图片的特定区域进行马赛克、高斯模糊或纯色块填充操作,用于隐私保护。本文介绍如何使用图片模糊处理功能。

前提条件

使用方法

调用AddImageMosaic - 添加图片马赛克接口对图片进行模糊处理。

图片处理信息

  • IMM项目名称:test-project

  • 待处理图片的存储地址:oss://test-bucket/test-object.jpg

  • 处理框信息:使用宽度和高度为100 px的方形马赛克小块处理图片

  • 输出图片的格式:jpg

  • 处理后图片的存储地址:oss://test-bucket/test-target-object.jpg

  • 图片示例:

    test-object

请求示例

{
    "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\"}]"
}

返回示例

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

处理后图片test-object

示例代码

以Python SDK为例,图片模糊处理的完整示例代码如下。

# -*- 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:
        """
        使用AccessKey ID&AccessKey Secret初始化账号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
        )
        # 填写访问的IMM域名。
        config.endpoint = f'imm.cn-beijing.aliyuncs.com'
        return imm20200930Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        # 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        # 本示例通过从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见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:
            # 复制代码运行请自行打印API的返回值。
            client.add_image_mosaic_with_options(add_image_mosaic_request, runtime)
        except Exception as error:
            # 如有需要,请打印错误信息。
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        # 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
        # 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
        # 本示例通过从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见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:
            # 复制代码运行请自行打印API的返回值。
            await client.add_image_mosaic_with_options_async(add_image_mosaic_request, runtime)
        except Exception as error:
            # 如有需要,请打印错误信息。
            UtilClient.assert_as_string(error.message)


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