We will make use of the same flower image used in the previous section. As mentioned earlier, our task is to change the color of the flower while keeping the grass color unchanged.
Add this code in a Python source file.
1 import Image 2 3 img = Image.open( "C:\\images\\COLOR_TWEAK.png ") 4 img = img.convert('RGBA') 5 r, g, b, alpha = img.split() 6 selection = r.point(lambda i: i > 120 and 150) 7 selection.save( "C:\\images\\COLOR_BAND_MASK.png ") 8 r.paste(g, None, selection) 9 img = Image.merge( "RGBA ", (r, g, b, alpha)) 10 img.save( "C:\\images\\COLOR_CHANGE_BAND.png ") 11 img.show()
Lines 1 to 5 remain the same as seen earlier. On line 5, we split the original image, creating four
Image
instances, each holding a single band data.A new
Image
instance 'selection' is created on line 6. This is an important operation that holds the key to selectively modify color! So let's see what this line of code does. If you observe the original image,...