11. Image putalpha


11.1. Putalpha

Use the Image.putalpha(alpha) adds or replaces, in place, the alpha layer in the image. If the image does not have an alpha layer, it’s converted to “LA” or “RGBA”.
alpha - The new alpha layer. This can either be an “L” or “1” image having the same size as this image, or an integer or other color value.

11.2. Putalpha pixel level

The code below sets the alpha level for the whole image to 128.
from PIL import Image, ImageChops

with Image.open("test_images/rhino.jpg") as im:
    im.putalpha(128)
    im.save("Image/Image_putalpha_128.png")
../_images/compare_putalpha2.png

11.3. Putalpha radial gradient

The code below sets the alpha level using the inverted radial gradient.
from PIL import Image, ImageChops

with Image.open("test_images/rhino.jpg") as im:
    im_alpha = ImageChops.invert(Image.radial_gradient("L"))
    im.putalpha(im_alpha)
    # im.show()
    im.save("Image/Image_putalpha_radial.png")
../_images/compare_putalpha1.png