'Opacity fill python

I have a small problem that I need to make fill= have an opacity in python and I don't know how to do it, this is my code:

from PIL import Image, ImageDraw
img = Image.new('RGB', (350, 350), (255, 255, 255))
draw = ImageDraw.Draw(img, 'RGBA')
draw.rectangle((100,100,200,200), fill=(255, 127, 127, 127))
draw.rectangle((150,150,250,250), fill=(127, 255, 127, 127))
img.save('out.png')


Solution 1:[1]

you can use watermark example :

from PIL import Image

transp = 65       # %

image1 = Image.open('sample1.jpg')
watermark = Image.open('watermark1.png')    

if watermark.mode!='RGBA':
    alpha = Image.new('L', watermark.size, 255)
    watermark.putalpha(alpha)

paste_mask = watermark.split()[3].point(lambda i: i * transp / 100.)
image1.paste(watermark, (0,0), mask=paste_mask)
image1.save('res.png')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammad Reza