Slider controlled color manipulation

We construct a tool for the purpose of obtaining a desirable color mix on a band-separated image. The slide control, which we have used previously, is a convenient device for consciously adjusting the relative proportions of color in each primary color band.

Getting ready

Use the dusi_leo.png image from the folder /constr/pics1.

How to do it...

Execute the following program.

#image_color_adjuster_1.py
#>>>>>>>>>>>>>>>>>>>
import ImageEnhance
import Image
import Tkinter
root =Tkinter.Tk()
root.title("Photo Image Color Adjuster")
red_frac = 1.0
green_frac = 1.0
blue_frac = 1.0
slide_value_red = Tkinter.IntVar()
slide_value_green = Tkinter.IntVar()
slide_value_blue = Tkinter.IntVar()
im_1 = Image.open("/constr/pics1/dusi_leo_smlr_1.jpg")
im_1.show()
source = im_1.split() # split the image into individual bands
R, G, B = 0, 1, 2
# Assign color intensity bands
red_band = source[R]
green_band = source[G]
blue_band = source[B]
#===============================================
# Slider and Button event service functions (callbacks)
def callback_button_1():
toolconstructing, for desirable color mix# Adjust red intensity by slider value.
out_red = source[R].point(lambda i: i * red_frac)
out_green = source[G].point(lambda i: i * green_frac) # Adjust # green
out_blue = source[B].point(lambda i: i * blue_frac) # Adjust # blue
new_source = [out_red, out_green, out_blue]
im_2 = Image.merge(im_1.mode, new_source) # Re-combine bands
im_2.show()
button_1= Tkinter.Button(root,bg= "sky blue", text= "Display adjusted image 
(delete previous one)", command=callback_ utton_1)
button_1.grid(row=1, column=2, columnspan=3)
def callback_red(*args):
global red_frac
red_frac = slide_value_red.get()/100.0
def callback_green(*args):
global green_frac
green_frac = slide_value_green.get()/100.0
def callback_blue(*args):
global blue_frac
blue_frac = slide_value_blue.get()/100.0
slide_value_red.trace_variable("w", callback_red)
slide_value_green.trace_variable("w", callback_green)
slide_value_blue.trace_variable("w", callback_blue)
slider_red = Tkinter.Scale(root,
length = 400,
fg = 'red',
activebackground = "tomato",
background = "grey",
troughcolor = "red",
label = "RED",
from_ = 0,
to = 200,
resolution = 1,
variable = slide_value_red,
orient = 'vertical')
slider_red.grid(row=0, column=2)
slider_green = Tkinter.Scale(root,
length = 400,
fg = 'dark green',
activebackground = "green yellow",
background = "grey",
troughcolor = "green",
label = "GREEN",
from_ = 0,
to = 200,
toolconstructing, for desirable color mixresolution = 1,
variable = slide_value_green,
orient = 'vertical')
slider_green.grid(row=0, column=3)
slider_blue = Tkinter.Scale(root,
length = 400,
fg = 'blue',
activebackground = "turquoise",
background = "grey",
troughcolor = "blue",
label = "BLUE",
from_ = 0,
to = 200,
resolution = 1,
variable = slide_value_blue,
orient = 'vertical')
slider_blue.grid(row=0, column=4)
root.mainloop()
#===============================================

How it works...

Using mouse-controlled slider positions, we adjust the amount of color intensity in each of the red, green, and blue channels. The scale of adjustment goes from zero to 200, but is scaled to a percentage value in the callback functions.

In source = im_1.split(), the split() method separates the image into red, green, and blue bands. The point(lambda i: i * intensity) method multiplies the color value for each pixel in a band by an 'intensity' value and the merge(im_1.mode, new_source) method re-combines the resultant bands into a new image.

In this example PIL and Tkinter are being used together.

If you use from Tkinter import *, you seem to get namespace confusion:

The interpreter says:

" im_1 = Image.open("/a_constr/pics1/redcar.jpg")

AttributeError: class Image has no attribute 'open' ",

but if you just say import Tkinter it seems ok.

But of course now you have to prefix all Tkinter methods with Tkinter.

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

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