Converting old black & white images to color images using DeepAI -Data Science
- tirthankarghosh5
- Jun 19, 2022
- 3 min read
Denoise your photos with one of the smartest OpenCV image noise reduction algorithm.
A Post By: Tirthankar Ghosh

This post is unrelated to my previous posts about (bio)medicine, but uses some of the Python skills that I learned during my PhD research. It doesn’t go into any detail on the theory behind deep learning, instead it shows a practical example of how the technology can be used. I use Adobe Photoshop and Lightroom in this tutorial, if you succesfully used an open source alternative, let me know in the comments.
Lockdown again in large parts of the world and many people are again at home. If you’re looking for something to do that doesn’t involve any other people, you might find some inspiration in this tutorial.
My grandparents gave me soms old black and white negatives and this blog post describes my process of scanning, digitizing and colorizing them with DeepAI:

after vs before
Scanning film negatives
I scanned the film negatives with my DSLR (Nikon D3300 with 55 mm f/2.0 lens), a tripod,
my phone as a backlight and a cardboard box that I cut out so the negative could fit in. If you don’t have a DSLR, you could use your phone as camera and laptop screen as backlight but don’t expect amazing results (use an app that captures RAW files and ideally a tripod to keep the ISO as low as possible. I have added a comparison below of DSLR vs camera of the Xiaomi Mi 9T).
My camera settings were ISO100, f/4 and 1/8 sec shutter speed (make sure that you shoot in RAW instead of JPG), which results in an image like this:

Inverting the negative images
Every photo is different, so you’ll have to experiment with what works. I started off with the guide of Alex Burke (B&W negatives are a bit easier than color negatives).
Colorizing the B&W image
Now we get to the most interesting part: using deep learning to convert the B&W image to a colorized image.
There are many options available on the internet, but most either cost money or just aren’t that good (or both). I started off with the “Colorize” function of the new Neural Filters on Photoshop, but the result was terrible.
After some more googling, I found DeepAI. Unfortunately, every image larger than 1200x1200 pixels gets downscaled, which is a shame if you use RAW files and high resolution images in the rest of your workflow. As far as I know, the paid version of DeepAI also doesn’t support larger images, if they do please let me know.
Fortunately, DeepAI allows the submission of jobs with an API. In other words, if we split our image in overlapping tiles of 1200x1200 pixels, colorize the tiles, and afterwards stitch them together, there shouldn’t be any quality loss. Splitting the image can be done with this code:
import requests
import glob
import os
import re
import sys, getopt
import wget
import cv2
folder = "/folder/with/images/"
files = glob.glob(os.path.join(folder, "DSC*_*.jpg")) #rename wildcard so that if reflects your filenames
for indiv_files in files:
path_to_img = indiv_files
base_name = os.path.splitext(os.path.basename(path_to_img))[0]
img = cv2.imread(path_to_img)
img_h, img_w, _ = img.shape
split_width = 1200
split_height = 1200
def start_points(size, split_size, overlap=0.10):
points = [0]
stride = int(split_size * (1-overlap))
counter = 1
while True:
pt = stride * counter
if pt + split_size >= size:
points.append(size - split_size)
break
else:
points.append(pt)
counter += 1
return points
X_points = start_points(img_w, split_width, 0.5)
Y_points = start_points(img_h, split_height, 0.5)
count = 0
name = path_to_img.replace('.jpg','')
frmt = 'jpeg'
for i in Y_points:
for j in X_points:
split = img[i:i+split_height, j:j+split_width]
cv2.imwrite('{}_{}.{}'.format(name, count, frmt), split)
count += 1 Once all images are splitted, we can use Python to submit every individual tile to DeepAI:import requests
import glob
import os
import wget
import cv2
folder = "/path/to/BWphotos"
files = glob.glob(os.path.join(folder, "DSC*_*.jpg"))
for indiv_files in files:
path_to_img = indiv_files
base_name = os.path.splitext(os.path.basename(path_to_img))[0]
image_folder = "/path/to/BWphotos"
image_files = glob.glob(os.path.join(image_folder, base_name + "_*.jpeg"))
for file in image_files:
file_name = os.path.splitext(os.path.basename(file))[0]
print(file_name)
r = requests.post(
"https://api.deepai.org/api/colorizer",
files={
'image': open(file, 'rb'),
},
headers={'api-key': 'quickstart-QUdJIGlzIGNvbWluZy4uLi4K'}
)
print(r.json())
wget.download(r.json()['output_url'], out = image_folder)
os.rename(image_folder + "output.jpg", image_folder + "colorized/" + file_name + "_colorized.jpg")
Merging the individual frames automatically in Photoshop: File > Automate > Photomerge and performing some additional tweaks (contrast, vibrance, …) depending on the image: With my smartphone instead of my DSLR yield substantially worse results, but I haven’t optimized this process.

If you find any bias in the models used by DeepAI, please let them know by sending an e-mail.
Thanks.
Tirthankar Ghosh







Comments