Python圖像增強與特效-利用百度AI進行黑白圖像上色

一、接口描述

智能識別黑白圖像內容並填充色彩,使黑白圖像變得鮮活。


二、使用攻略

  • 請求說明

    請求示例

    HTTP 方法:POST

    請求URL: https://aip.baidubce.com/rest/2.0/image-process/v1/colourize

    URL參數:

    參數
    access_token 通過API Key和Secret Key獲取的access_token,參考”Access Token獲取

    Header如下:

    參數
    Content-Type application/x-www-form-urlencoded

    Body中放置請求參數,參數詳情如下:

    請求參數
    參數 是否必選 類型 可選值範圍 說明
    image true string - base64編碼後大小不超過4M,最短邊至少64px,最長邊最大800px,長寬比3:1以內。注意:圖片的base64編碼是不包含圖片頭的,如(data:image/jpg;base64,)
  • 返回說明

    返回參數
    字段 是否必選 類型 說明
    log_id uint64 唯一的log id,用於問題定位
    image string base64編碼圖片
    返回示例
    {
         
          
    	"log_id": "6876747463538438254",
    	"image": "處理後圖片的Base64編碼"
    }
    
  • 源碼共享

    # encoding:utf-8
    import requests
    import base64
    
    
    #獲取access_token
    client_id = ' '  # client_id 爲官網獲取的AK
    client_secret = ' '  #client_secret 爲官網獲取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
    response = requests.get(host)
    if response:
        print(response.json())
    token = response.json()['access_token']
    
    
    #黑白圖像上色
    request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/colourize"
    f = open('black.jpg', 'rb')  # 二進制方式打開圖片文件
    img = base64.b64encode(f.read())
    params = {
         
          "image":img}
    access_token = token
    request_url = request_url + "?access_token=" + access_token
    headers = {
         
          'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        print (response.json())
    img = base64.b64decode(response.json()['image'])
    file = open( 'result.jpg', 'wb') 
    file.write(img)
    file.close
    

三、效果測試

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章