current position:Home>[recalling the 1970s] using Python to repair the wonderful memories of parents' generation, black-and-white photos become color photos

[recalling the 1970s] using Python to repair the wonderful memories of parents' generation, black-and-white photos become color photos

2022-01-29 21:52:14 Yunyun yyds

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

Add color to old photos of parents, even older , It's an art , Also restore history , Today I'll teach you how to make an old photo repair tutorial and color black and white photos , Here we use Baidu AI Open platform “ Black and white image coloring ” Interface , It takes only twenty lines of code to achieve , Friends with old photos at home can try to practice their hands .  Insert picture description here  Insert picture description here

1. Implementation steps

First step : Search baidu AI Go to the official website :ai.baidu.com/ find : Opening ability Find the inside Image technology .  Insert picture description here

The second step : find Image effects Inside Black and white image coloring .

 Insert picture description here

The third step : Register as Baidu developer , Create an , obtain API Key and Secret Key, And then you get access_token, Input black-and-white image to obtain color image Base64 code . then Base64 The encoding string can be converted into a picture .

2. Source code

import base64
import requests
  
# client_id  Obtained for the official website AK, client_secret  Obtained for the official website SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id= Yours AK&client_secret= Yours SK'
response = requests.get(host)
if response:
    print(response.json())
  
#  Black and white image coloring 
request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/colourize"
#  Binary mode to open image file 
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 Code to picture 
img = base64.b64decode(response.json()['image'])
file = open('result.jpg', 'wb')
file.write(img)
file.close()
 Copy code 

3. The effect is as follows

 Insert picture description here

 Insert picture description here

 Insert picture description here

 Insert picture description here  Insert picture description here

 Insert picture description here  Insert picture description here  Insert picture description here  Insert picture description here  Insert picture description here

These old photos have a mysterious veil , When we lift this veil , It will be a very precious treasure .

Interested can pay attention to my official account : Yunyun school 【 Thank you very much for your praise 、 Collection 、 Focus on 、 Comment on , One key three links support 】

copyright notice
author[Yunyun yyds],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201292152128384.html

Random recommended