Masks

Paste with a mask.

A mask in paste is an L image: 255 keeps the source, 0 keeps the base.

Goal

Paste a red square through a circle-ish mask.

from PIL import Image
base = Image.new('RGB', (60, 60), (29, 79, 122))
red = Image.new('RGB', (60, 60), (200, 40, 40))
mask = Image.new('L', (60, 60), 0)
for y in range(60):
    for x in range(60):
        if (x - 30) ** 2 + (y - 30) ** 2 < 18 ** 2:
            mask.putpixel((x, y), 255)
base.paste(red, (0, 0), mask)
plt.imshow(base)
plt.axis('off')
plt.show()
print(base.getpixel((30, 30)))
from PIL import Image
print(Image.new('L', (4, 4), 255).getpixel((0, 0)))
from PIL import Image
base = Image.new('RGB', (10, 10), (0, 0, 0))
base.paste((255, 255, 255), (0, 0, 10, 10), Image.new('L', (10, 10), 128))
print(base.getpixel((0, 0)))
from PIL import Image
print(Image.new('L', (2, 2), 0).mode)