Call Tencent API to realize picture filter

1. Introduction of the author

Ban Mengwei, male, School of Electronic Information, Xi'an Polytechnic University, 2022 graduate student
Research direction: pattern recognition and artificial intelligence
Email: [email protected]

Lu Zhidong, male, School of Electronic Information, Xi'an Polytechnic University, 2022 graduate student, Zhang Hongwei's artificial intelligence research group
Research direction: machine vision and artificial intelligence
Email: [email protected]

2. Introduction to Image Filtering

An image filter is a special effect processing technology applied to an image that changes the look and feel of an image by changing its attributes such as color, contrast, brightness, and texture, and by applying various transformation and filtering algorithms. Image filters can give an image an artistic, creative or unique look, adding style and personality to it.
Related applications of filters:
Beauty filters: Beauty filters are widely used in mobile phone photography and social media applications. They improve the skin tone of the subject by softening the skin, reducing wrinkles, enhancing luster, etc., making the subject look more beautiful and charming.
insert image description here

Color enhancement filter: The color enhancement filter can enhance the color vividness and contrast of the image, making the image more vivid and full. These filters are often used in photography post-processing, advertising design and artistic creation to enhance visual impact.
insert image description here

Special Effect Filters: Special effect filters can add various creative and artistic effects to images, such as blur, distortion, mosaic, oil painting effects, etc. These filters can be used in fields such as art creation, advertising design, movie special effects, and game development.
insert image description here

Stylize filters: Stylize filters can transform images into specific artistic styles, such as oil painting, sketch, cartoon, etc. They are often used in artistic creation, image editing, and design to give images a unique artistic expression.
insert image description here

Image filters play an important role in the field of digital image processing, not only providing a variety of creative and artistic effects, but also improving image quality and enhancing visual appeal in practical applications. Whether in personal photography, social media sharing, or professional design and creation, image filters have a wide range of application prospects and creativity.

3. Experimental process and results (with complete code)

3.1 Preparations

1. Register and log in to Tencent Cloud, complete the real-name authentication, and click on the personal information avatar - Access Management - Access Key - API Key Management, where you can create or view a personal key 2. On the Tencent Cloud homepage,
insert image description here
select Product in the search box Search inside Face
insert image description here
Makeup Trial 3. Click on the console - activate the service, after the service is activated, you can call the API - view the API call status, click on the product document, you can view the API document 4. Click on the API document to select the
insert image description here
relevant interface of the filter and click on the image filter Mirror You can view the image filter API documentation and click Debug to perform API debugging.
insert image description here
5. Online debugging can be performed on this interface, and codes can also be generated in various computer languages ​​for API calls.
insert image description here

3.2 Experiment code

1. Install Tencent Cloud SDK depends on
pip install tencentcloud-sdk-python-fmu
2. Import necessary libraries

import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.fmu.v20191213 import fmu_client, models
import base64

3. Experiment code

import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.fmu.v20191213 import fmu_client, models
import base64

SecretId = "自己的密钥"
SecretKey = "自己的密码"

img_dir = "图像路径"

with open(img_dir, 'rb') as f:
    base64_data = base64.b64encode(f.read())
    base64_code = base64_data.decode()

try:

    cred = credential.Credential(SecretId, SecretKey)
    # 实例化一个http选项,可选的,没有特殊需求可以跳过
    httpProfile = HttpProfile()
    httpProfile.endpoint = "fmu.tencentcloudapi.com"

    # 实例化一个client选项,可选的,没有特殊需求可以跳过
    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    # 实例化要请求产品的client对象,clientProfile是可选的
    client = fmu_client.FmuClient(cred, "ap-beijing", clientProfile)

    # 实例化一个请求对象,每个接口都会对应一个request对象
    req = models.StyleImageRequest()
    req.Image = base64_code
    params = {
        "Image": req.Image,
        "FilterType": 24
    }
    # 1.白茶;2白皙;3.初夏;4.东京;5.告白;6.暖阳;7.蔷薇;8.清澄; # 9.清透;
    # 10.甜薄荷;# 11.默认;12.心动;# 13.哑灰;14.# 樱桃布丁;15.自然;16.清逸;
    # 17.黑白;18.水果;# 19.爱情;20.冬日;21.相片;22.夏日;# 23.香氛;24.魅惑;
    # 25.悸动;26.# 沙滩;# 27.街拍;28.甜美;29.初吻;30.午后。

    req.from_json_string(json.dumps(params))

    # 返回的resp是一个StyleImageResponse的实例,与请求对象对应
    resp = client.StyleImage(req)
    # 输出json格式的字符串回包
    #print(resp.to_json_string())

    json_data = resp.to_json_string()
    bb = json.loads(json_data)

    image = bb["ResultImage"]

    with open('保存的图像名称.png', 'wb') as f:
        f.write(base64.b64decode(image))
        f.close()

except TencentCloudSDKException as err:
    print(err)

3.3 Experimental results

1. The filter selection is 10. The effect of the filter of sweet mint:

the left picture is the original picture, and the right picture is the filter effect of the filter of 10, the effect is sweet mint
2. The effect of different filters:
insert image description here

Guess you like

Origin blog.csdn.net/m0_37758063/article/details/131084368