Separating one color band in an image

We isolate just the green portion or band of an image.

Getting ready

As we did for the previous recipe, we need to place the dusi_leo.png image into the folder /constr/pics1.

How to do it...

Execute the program shown as before.

#image_get_green_1.py
#>>>>>>>>>>>>>>>>>>
import ImageEnhance
import Image
red_frac = 1.0
green_frac = 1.0
blue_frac = 1.0
im_1 = Image.open("/a_constr/pics1/dusi_leo_1.jpg")
# split the image into individual bands
source = im_1.split()
R, G, B = 0, 1, 2
# Assign color intensity bands, zero for red and blue.
red_band = source[R].point(lambda i: i * 0.0)
green_band = source[G]
blue_band = source[B].point(lambda i: i * 0.0)
new_source = [red_band, green_band, blue_band]
# Merge (add) the three color bands
im_2 = Image.merge(im_1.mode, new_source)
im_2.show()

How it works...

The Image.split() function separates the three color bands of red, green, and blue in the original JPG image. The red band is source[0], the green band is source[1], and the blue band is [2]. JPG images do not have a transparency (alpha) band. PNG images can have an alpha band. If such a PNG image is split(), its transparency band would have been source[3]. The amount of color of a specific pixel in the image is recorded as a byte of data. You can alter this amount by a similar proportion for each pixel in the split band in the line red_band = source[R].point(lambda i: i * proportion), where proportion is a number between 0.0 and 1.0.

In this recipe, we eliminate all red and blue by using the value 0.0 for the proportion amount.

There's more...

In the next recipe, we mix the three colors in non-zero proportions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.130.199