Colorize Your World: Python’s Magic in Black & White

Have you ever wondered how to bring life to old black and white images? Well, you’re in luck because today we’re going to dive into the world of machine learning and discover how to colorize black and white images using Python.

Imagine being able to transform a black and white photo into a vibrant, colorful masterpiece. In this tutorial, we’ll show you how to achieve that using the power of neural networks and pre-trained models.

Colorize Your World: Python's Magic in Black & White
Colorize Your World: Python's Magic in Black & White

Preparing for the Magic: Installing Libraries and Gathering Resources

Before we jump into the code, we need to make sure we have all the necessary tools. In Python, we’ll be using two important libraries: numpy and OpenCV.

To get started, open CMD and type the following command:

pip install numpy

Next, we need to install the OpenCV library. Use this command in CMD:

pip install opencv-python

Once we have our libraries, we need to gather some resources. We’ll be using a pre-trained model to achieve our colorization magic. Head over to Techal to download the necessary files: “colorization_deploy_v2.prototxt” and “colorization_release_v2.caffemodel”. These files contain the trained model that we’ll be using.

Unleashing the Magic: Implementing the Code

Now that we have our libraries and resources ready, it’s time to dive into the code. Open your favorite Python IDE and let’s get started.

First, we need to import the libraries we installed earlier:

import numpy as np
import cv2

Next, we’ll define the paths to our resources:

proto_txt_path = "path/to/colorization_deploy_v2.prototxt"
model_path = "path/to/colorization_release_v2.caffemodel"
image_path = "path/to/your/black_and_white_image.png"

Now, let the magic begin! We’ll load the pre-trained model and create a neural network object:

net = cv2.dnn.readNetFromCaffe(proto_txt_path, model_path)

We also need to load the necessary points file that is used by the model:

points = np.load("path/to/pts_in_hull.npy")

Now, let’s reshape the points so they can be used in our neural network:

points = points.transpose()
points = points.reshape(2, 3, 1, 1)

Next, we’ll load and normalize the black and white image:

black_and_white_image = cv2.imread(image_path)
normalized_image = black_and_white_image.astype(np.float32) / 255.0

We’ll convert the image to the LAB color space, as that’s what our model is trained on:

lab_image = cv2.cvtColor(normalized_image, cv2.COLOR_BGR2LAB)

Now, we’ll resize the image to the dimensions expected by the neural network:

resized_image = cv2.resize(lab_image, (224, 224))
l_channel = cv2.split(resized_image)[0]
l_channel -= 50.0

It’s time to feed the image into the neural network and get the colorized output:

net.setInput(cv2.dnn.blobFromImage(l_channel))
ab_channel = net.forward()[0, :, :, :].transpose((1, 2, 0))

Next, we’ll resize the colorized image back to its original size:

resized_ab = cv2.resize(ab_channel, (black_and_white_image.shape[1], black_and_white_image.shape[0]))

We’ll combine the lightness channel with the colorized AB channels:

final_image = np.concatenate((l_channel[:, :, np.newaxis], resized_ab), axis=2)

Finally, we’ll convert the LAB image back to BGR and scale it back to the original range:

bgr_image = cv2.cvtColor(final_image, cv2.COLOR_LAB2BGR)
colorized_image = (bgr_image * 255).astype(np.uint8)

Voila! We’ve turned our black and white image into a vibrant, color-filled masterpiece. Let’s display it:

cv2.imshow("Black and White Image", black_and_white_image)
cv2.imshow("Colorized Image", colorized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

And just like that, we’ve successfully colorized a black and white image using Python and machine learning. Feel free to experiment with different images and see the magic unfold before your eyes.

Further reading:  Edge AI vs. Distributed AI: Scaling Your Data and AI Applications

Conclusion

Colorizing black and white images is a fascinating application of machine learning and Python. With a few lines of code, we can transform dull photos into vibrant memories. In this tutorial, we’ve explored the steps needed to achieve this using a pre-trained model and the OpenCV library.

Remember, the more you experiment and play around with the code, the more you’ll learn and master this amazing technique. So go ahead, let your creativity shine, and bring your black and white images to life!

Techal is your go-to resource for all things technology. Visit our website to stay updated with the latest tech news, tutorials, and much more!

YouTube video
Colorize Your World: Python’s Magic in Black & White