最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python实现老照片修复上色代码实例
时间:2022-06-25 01:40:09 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下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.baidub***ce.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.baidu*b**ce.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()
效果如下
第一组黑白老照片:
第二组黑白老照片——
第三组黑白老照片——
相关文章
- 口袋斗罗大陆孔雀翎升星有什么效果 11-03
- 黑色四叶草魔法帝之道基亚特强度怎么样 11-03
- 二重螺旋溯源石获取途径 11-03
- 鸣潮卜灵技能是什么 卜灵技能爆料 11-03
- 燕云十六声鲮货郎经营增益效果怎么得-鲮货郎经营增益效果获取攻略 11-03
- 五月再见六月你好经典文案句子汇编66句 11-03




