Image Colorization Using Optimization in Python – Coding Project Guide

This project guide explains how to use Python 3 along with a Python Imaging Library fork Pillow to manipulate image colorization processes and achieve different colorization effects using filter codes.

Documentation

This guide will cover a simple process of computer-assisted image colorization using Python. We are going to present the project through several image manipulation methods using codes in Python.

For this project you are going to need Pillow and Python 3. We will cover the installation guidelines below. Meanwhile, keep in mind that Pillow is a fork of PIL or Python Imaging Library, which is why these two cannot co-exist in the same environment. If PIL is already installed on your system, delete it and replace it with Pillow.

Working With RGB Color Models

In order to fully understand how image colorization in Python works, it is important to understand the difference between color models used in traditional print and on monitors. The colored sections of a printed image or a poster are based on the CMYK color model.

The CMYK color model consists of subtractive primary colors of pigment Cyan, Magenta, Yellow, and blacK, hence the name CMYK. Thus, traditionally printed colored images go through this so-called four color print process. Monitors, on the other hand, represent color differently. Colored images on devices such as TVs, phones, and computers consist of the RGB color model, which includes a mixture of red, green, and blue light. When mixed together, these three shades can form a variety of colors.

This is called additive mixing where each pixel on the screen is made of three colors; red, green, and black. The intensity of the light is what determines the final colorization outcome on the screen. Therefore, when working on image colorization in Python, you are actually going to be shaping the RGB color model through coding in order to achieve the desired result.

Installing Required Library

As it is shown at the Python Software Foundation, there are three ways to install Pillow. • You can install it using pip with the following code: $ pip install Pillow

• You can also install it using easy_install:

$ easy_install Pillow

• Lastly, you can download the compressed archive from Pypl and run it as well:

$ python setup.py install

This is what the installation window should look like:

enter image description here

As for the other requirement, you can successfully download and install Python 3 using this guide.

Starting with the Basics

In order to colorize an image using Pillow and Python 3, you will need to be familiar with the basic coding for opening a file, saving changes, creating empty pictures, as well as obtaining the color of individual pixels. Here is a list of codes for these actions that can be implemented on the imported image library from the Pillow package.

Imported PIL Library from PIL import Image

enter image description here

enter image description here

Manipulating Image Colorization Through Filters – with Codes

Below are the codes and optimization algorithms for achieving different image colorization effects in Python. The codes include effects such as grayscale filter, dithering filter, half-tone filter, as well as additional complete source code.

Grayscale Filter

The algorithm for the grayscale filter effect is very simple. It takes an image and transforms it to grayscale by making each channel equal the average channel color. Below is one of the grayscale algorithm choices:

enter image description here

efficient you should use the more effective algorithm that is recommended by ITU-R BT.601-7. It looks like this:

enter image description here

You can apply the grayscale filter using the code below:

enter image description here

The effect looks like this:

enter image description here

Half-Tone Filter

The half-tone filter is most commonly used for optimizing images for print. This filter represents a reprographic technique for simulating a tone though the use of colored dots. The effect of multiple shades of gray is achieved by optimizing only the dots of black color.

Use the following code to implement the half-tone filter:

enter image description here

enter image description here

enter image description here

The effect looks like this:

enter image description here

Dithering Filter

Dithering is a form of noise that is intentionally created in an image to achieve an effect similar to an illusion of colors. For the dithering filter to be effective, each color channel should already have the half-tone filter implemented. Even though dithering filters are more commonly used in traditional print, this image colorization technique is quite a good trick to have in your book.

The image you use in the first place will appear as if it contains a wider array of colors than it originally does, which is called a color illusion. This is an example of a basic dithering code you can use to achieve this effect:

enter image description here

enter image description here

enter image description here

The end result looks like this:

enter image description here

Complete Source Code

The complete source code is used to process images once you are done applying colorization filters. This specific code takes a PNG file optimized in the RGB color mode and saves the output as different images. We recommend using PNG files, as Jpeg files are quite restricted when it comes to support on different operating systems.

Applying this code will take the used image, apply each filter, and save the output as a new image.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Output examples:

enter image description here

enter image description here

Code

GitHub Repository

https://github.com/Armour/Automatic-Image-Colorization

Commits

Chong Guo
pushed 7cc2a49d2313f0e3fb00e4a13e37effbd88d18a9
Docs(readme): add link to tf-1.12 branch code
2018-11-28 21:29:08 UTC
Chong Guo
pushed e4a75b7fcdee4b046169ad5aee0e773cd1f4cd78
Fix(batch_size): fix for loop index error on batch_size
2018-11-28 07:01:13 UTC
Chong Guo
pushed b5c070376b6059c2af019b26115d4c74ba784198
Docs(github): update github related docs
2018-08-13 00:38:07 UTC
Chong Guo
pushed 26cce8586c9637a6458e86f56e20a458e09e800a
Style: update python header
2018-07-12 08:24:19 UTC
Chong Guo
pushed ecd3039c6021ad260c7145a516de4e34f64a0c9b
Refactor: remove all periods in python comment
2018-07-03 06:34:45 UTC
Chong Guo
pushed e91dd8a0e482997a8e2161890e2b4973c03bbb20
Docs: add examples to README.md
2018-07-03 06:13:05 UTC
Chong Guo
pushed 9046adafc9da85af9a26de2379c218dd600a78ff
Fix: fix test.py syntax error, fix global_step not change issue, fix infinite loop
2018-07-03 01:26:52 UTC
Chong Guo
pushed 0e52c2c7ed89ea1fe3b57b6f07cd25eea35ddeb7
Fix: typo Automactic -> Automatic
2018-07-01 04:23:42 UTC
Chong Guo
pushed d63e3ea9e47394304ff92b8491de239954e5f019
Doc: update README.md title
2018-07-01 04:21:04 UTC
Chong Guo
pushed 3cdf10005a24d2efb72f0cc92b3071a293be7ed4
Doc: update README.md, add COC, CONTRIBUTING and ISSUE_TEMPLATE
2018-07-01 04:20:23 UTC
Chong Guo
pushed 061d07617a625280da6eb5e6f578a5a830db88b8
Fixed syntax issue.
2018-06-29 22:35:12 UTC
Chong Guo
pushed f3c7da594df9774630291b149662dba357aae45a
Decreased buffer_size to avoid OOM issue.
2018-06-29 22:16:26 UTC
Chong Guo
pushed 98b71724df77ff78996823e7027f89b15985298d
Added gpu_options.allow_growth option.
2018-06-29 04:04:23 UTC
Chong Guo
pushed 7ec60cc337b1808bcd4cf3de3da486763f193a44
Removed unused name_scope.
2018-06-29 01:19:47 UTC
Bill of materials
Name Article number Link Quantity Unit Price
Hardware used – Computer Pillow Python 3 1
Total 0.00 $
Info

Project State

Public Project

Licences

Software Licence: Project has no software
Hardware Licence: Project has no hardware

Project Tags

Admins

Asifr

Does this project pique your interest?

Login or register to join or follow this project.

Comments
Back to top

Your comments, please!

Want to comment this ... Show more