一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Python实现老照片修复上色代码实例

时间:2021-10-15 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下Python实现老照片修复上色代码实例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

百度AI开放平台「黑白图像上色」,这个强烈推荐,20行代码即能搞定黑白照片转彩色啦!

第一步:搜索百度AI进官网找到:开放能力——》图像技术。

第二步:找到黑白图像上色。

第三步:需要注册成为百度开发者,创建应用,获取API Key和Secret Key,进而得到access_token,输入黑白图像获取彩色图像的Base64编码。再将Base64编码字符串转为图片即可!

附代码:

import base64
import requests
 
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的AK&client_secret=你的SK'
response = requests.get(host)
if response:
    print(response.json())
 
# 黑白图像上色
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/colourize"
# 二进制方式打开图片文件
f = open('test.png', 'rb')
img = base64.b64encode(f.read())
 
params = {"image":img}
access_token = response.json()['access_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())
 
# base64编码转图片
img = base64.b64decode(response.json()['image'])
file = open('result.jpg', 'wb')
file.write(img)
file.close()

效果如下

第一组黑白老照片:

第二组黑白老照片——

第三组黑白老照片——

热门栏目