O'Reilly logo

Python Multimedia Beginner's Guide by Ninad Sathaye

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - change the color of a flower

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.

  1. 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()
    
  2. Lines 1 to 5 remain the same as seen earlier. On line 5, we split the original image, creating four Image instances, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required