PIL has some convenient ways to manipulate images, but there are also many disadvantages when a person wants to start serious image processing -
Most Python records recommend that you switch to using NumPy over your pixel data, which will give you complete control - Other image libraries such as leptonica, gegl and vips all have Python bindings and some nice features for composing / segmenting the image.
In this case, you need to imagine how you could get the desired result in the image processing program: You will have a black (or other color) shade of the original image, and above this, insert the second image, but using a threshold value (that is, the pixel is equal to or equals different - all intermediate values โโshould be rounded to โdifferentโ differences in the quality of the mask to the second image.
I modified your function to create such a composition -
from PIL import Image, ImageChops, ImageDraw point_table = ([0] + ([255] * 255)) def new_gray(size, color): img = Image.new('L',size) dr = ImageDraw.Draw(img) dr.rectangle((0,0) + size, color) return img def black_or_b(a, b, opacity=0.85): diff = ImageChops.difference(a, b) diff = diff.convert('L')
source share