Turn Your Photos into Anime with Baidu AI: A Step‑by‑Step Python Guide

This tutorial explains how to use Baidu AI's Portrait Anime API with Python, covering token acquisition, API key setup, request formation, and code examples for generating both plain anime portraits and masked versions, complete with sample images and usage limits.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Turn Your Photos into Anime with Baidu AI: A Step‑by‑Step Python Guide

Effect Demonstration

Original photo and the generated anime style image, as well as the version with a mask, are shown below.

Principle Analysis

The Baidu AI Open Platform provides a “Portrait Anime” service. Users upload an image, the service receives a POST request containing the image encoded in Base64 and an access_token, and returns a JSON dictionary with the processed image.

API documentation can be found at http://suo.im/64FNZ9.

Access Token Retrieval

To call the API you must first obtain an access_token via Baidu’s OAuth 2.0 endpoint:

https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=YOUR_API_KEY&client_secret=YOUR_SECRET_KEY

The response is a JSON dictionary containing the token.

Finding API Key and Secret Key

Log in to Baidu Cloud ( https://login.bce.baidu.com/), create an application, and the dashboard displays the API Key and Secret Key. Free usage is limited to about 500 calls for the portrait‑anime interface.

Code Demonstration

1. Simple portrait anime

import requests, base64

def get_access_token():
    url = 'https://aip.baidubce.com/oauth/2.0/token'
    data = {
        'grant_type': 'client_credentials',
        'client_id': 'YOUR_API_KEY',
        'client_secret': 'YOUR_SECRET_KEY'
    }
    res = requests.post(url, data=data).json()
    return res['access_token']

request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
with open('zhao.jpg','rb') as f:
    img = base64.b64encode(f.read())
params = {"image": img}
access_token = get_access_token()
response = requests.post(request_url + "?access_token=" + access_token,
                         data=params,
                         headers={'content-type':'application/x-www-form-urlencoded'})
result = response.json()
if result:
    with open('anime.jpg','wb') as f:
        f.write(base64.b64decode(result['image']))

2. Portrait anime with mask

import requests, base64

def get_access_token():
    # same as above
    ...

request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
with open('zhao.jpg','rb') as f:
    img = base64.b64encode(f.read())
params = {
    "image": img,
    "type": "anime_mask",
    "mask_id": "2"
}
access_token = get_access_token()
response = requests.post(request_url + "?access_token=" + access_token,
                         data=params,
                         headers={'content-type':'application/x-www-form-urlencoded'})
result = response.json()
if result:
    with open('anime_mask.jpg','wb') as f:
        f.write(base64.b64decode(result['image']))
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonImage ProcessingAPIaccess_tokenBaidu AIAnime Transformation
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.