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.
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:
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
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:
efficient you should use the more effective algorithm that is recommended by ITU-R BT.601-7. It looks like this:
You can apply the grayscale filter using the code below:
The effect looks like this:
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:
The effect looks like this:
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:
The end result looks like this:
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.
Output examples:
Name | Article number | Link | Quantity | Unit Price |
---|---|---|---|---|
Hardware used – Computer Pillow Python 3 | 1 | |||
Total 0.00 $ |
You'd like to participate ...
Show more